0
votes

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)
1
give an example of what does it output, what is your desired output?chouaib
Perhaps try fopen(fname, "wb"); instead of just "w"? I'm not sure how you got it into this mode.Michael
Just a new line character. It outputting both new line characters and carriage returnscokedude
@Michael It does the same thing.cokedude
Michael's suggestion is the only thing you can do from a C program. What is your operating system environment? What is your compiler version?jxh

1 Answers

1
votes

"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.