0

I have my fopen set up like this. I have tried my fopen with both "t" and without "t". For some reason my fprintf is printing out ^M which are carriage returns. How do I stop frpintf from doing this? I want to just use the normal new line character.

FILE *outputfp;
// +5 to have space for .txt and terminator 
char *fname = calloc(strlen(argv[1]) + 5, sizeof(fname[0])); 
strcpy(fname, argv[1]);
strcat(fname, ".lst");
outputfp = fopen (fname, "w");
//printf("Above error message\n");
if (outputfp == NULL)
{
    printf("Error while opening the file.\n");
    return 1;
}
fprintf(outputfp, "hello\n");

http://en.wikipedia.org/wiki/Control_character http://www.pixhost.org/show/4770/21527928_cr.jpeg

I'm using Fedora and I compile this with gcc

Update:

    if((int)line[0] == 46)
    {
        //printf("You have a period \n");
        //printf("%s", line);
        for(i = 0; i < 80; i++)
        {
            //fprintf(outputfp,"%c", line[i]);
            if (isprint((unsigned char)line[i]) || isspace((unsigned char)line[i]))
             { 
             printf("%c", line[i]);
             //fprintf(outputfp, "\n Print something \n");
             fprintf(outputfp,"%c", line[i]);
             }

            //printf("%c", line[i]);
            //printf("  %d %c  ", line[i], line[i]);
        }
        //fprintf(outputfp, "\n ");
        //printf(" ------------------------\n");
        memset(line, 0, 80);
        comment_flag = 1; 
    }

        //sscanf(line, "%s %s %x", label, mneumonic , &start_address);
        //printf("start_address %d \n", start_address);
        printf("%x %s %s %s %x\n", start_address, label, mneumonic, operand, start_address);
        fprintf(outputfp, "%x %s %s %s %x\n", start_address, label, mneumonic, operand, start_address);

It actually looks like this is the line it doesn't like. I wanna cycle through my whole array before it prints a new line character.

fprintf(outputfp,"%c", line[i]);

Update:

char line[80] = {0};
while(fgets(line, 80, input) != NULL)
rmi
  • 532
  • 4
  • 15
cokedude
  • 379
  • 1
  • 11
  • 21

1 Answers1

1

"t" is the default. If you want binary mode then open with "wb".

A text stream (the default) may perform various conversions between the disk file and what your C program sees; a binary stream is meant to have a 1-1 character mapping (although this is all implementation-defined of course).

Update: Since you are working in Linux, probably there is no problem with text or binary mode.

Based on your output screenshot, it seems that you actually wrote a \r to your file. The first line doesn't have one. It'd help if you actually show the code which generates that output. Perhaps you are reading those lines in from a file which has \r\n line endings.

Update #2: It turns out that the \r characters are coming from a file that is being read in and then being output verbatim after passing through a filter if (isprint((unsigned char)line[i]) || isspace((unsigned char)line[i])).

The isspace function lets through all of " \t\n\v\f\r". You'll need to modify this check; perhaps you could also block out \r and \f; or alternatively, stop using isspace and just check for ' ' '\t', and reinstate your output of "\n" after the loop.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • I'm using Fedora and I compile this with gcc. Yes you are write with your fprintf statement. – cokedude Apr 24 '14 at 01:43
  • I show my print statements. It doesn't seem to like it when I print through my array. – cokedude Apr 24 '14 at 01:55
  • Where is `line` coming from in your code? I presume the block starting `line[0] == 46` corresponds to the output `. tests pc forward addressing`, so probably `line` already has the `\r` character in it. – M.M Apr 24 '14 at 01:57
  • line is my character array that gets filled by fgets as I read through a file. – cokedude Apr 24 '14 at 02:16
  • I used vi show the hidden characters that I'm reading. It all ends in $ which are new line characters. – cokedude Apr 24 '14 at 02:19
  • `vim` knows about Windows text files. Try hex-dumping the file (`od -Ax -t x1 filename`) . Updated my post again – M.M Apr 24 '14 at 02:21
  • Is there a way to make the hex dump more readable? It turns into a big jumbles mess. I tried to compare it to an ascii table but everything is out of order. – cokedude Apr 24 '14 at 03:20
  • Look for `0d 0a` in the output. That would be `\r\n`. You can use `more` to page it. – M.M Apr 24 '14 at 03:28