2

I have a c code, simply reads a line from a txt file. The file has only one line which is as below:

Content of txt file

The code snippet to read this line is:

int** readFile(char* filename){

    int col=0, row =0;
    int i=0;
    int* numList[2048];

    for(int i = 0; i<2048; i++)
        numList[i] = (int*) malloc(6*sizeof(int));

    if(NULL == numList){

        printf("Memory error!");

    }

    char * token = NULL;

    char currentLine[25] = {'\0'};

    FILE* file = fopen(filename, "r");

    if(NULL != file){

        printf("File is opened successfully\n");

        if( NULL != fgets (currentLine, 60, file) ) 
        {
            int i = 0;
            while (NULL != currentLine[i]){

                printf("%d ", currentLine[i]);
                i++;
            }
        }
    }
    else
    {
        printf("File I/O Error");
        return NULL;
    }

    fclose(file);

    return numList;
}

When this code runs, I get the following output:

Output of program

I observed something suspicious, which is, as you can see in the first screenshot (Content of txt file), Notepad++ shows CR LF at the end of the line. But in the output, I see 10 as the last character which is LF.

Probably I am missing a very primitive point but, I couldn't understand why CR character is not there.

Needless to say, platform is windows and this is a console program.

Thanks&Regards.

Ugur KAYA
  • 167
  • 3
  • 14
  • 4
    1) In the windows environment, mutual conversion (LF <-> CRLF) is done in text mode input / output. – BLUEPIXY Jul 25 '17 at 13:03
  • 4
    2) `60` <-> `25` are doesn't match size. – BLUEPIXY Jul 25 '17 at 13:04
  • 2
    You have declared `numList` as an array with automatic storage class (i.e. it is a local variable on the stack), and you have `return numList;` at the end of your function. This will return a pointer to a variable on the stack, which results in _undefined behavior_. – Ian Abbott Jul 25 '17 at 13:05
  • You really ought to read the man page for fgets, especially as it relates to newlines. – David Hoelzer Jul 25 '17 at 13:11
  • 2
    In fact... you really ought to make sure that you are not reading more data than the amount of memory that you have allocated... You're reading up to 60 bytes into a 25 byte array. – David Hoelzer Jul 25 '17 at 13:13
  • @Ian Abbott yes you are right, but I think it is another thing to discuss. Also the code is a little bit modified and some parts are removed for simplicity to ask the question. – Ugur KAYA Jul 25 '17 at 13:31

1 Answers1

10

You're opening the file in text mode. This mode ensures you can handle text files the same on any platform.

C specifies '\n' as the end of line. In Windows, the end of line is the sequence "\r\n". C (in this case, the standard library implementing stdio) will automatically translate this for you. Reading from a file on Windows in text mode will give you just \n for \r\n.

If you want to see exactly the byte contents of the file, you have to open it in binary mode instead:

FILE* file = fopen(filename, "rb");
  • Thank you! This is the answer I was looking for. – Ugur KAYA Jul 25 '17 at 13:32
  • 2
    @UgurKAYA YW. Still have a look at all the comments you received, there are quite some serious problems with the code snippet you show. I only answered your immediate question, ignoring these issues. –  Jul 25 '17 at 13:33
  • yes you are right. This is not the exact code in the project, it is little bit simplified. And also it is not the exact txt file for the project, but still, yes, there are some issues and I thank everyone depicting them. – Ugur KAYA Jul 25 '17 at 13:37