Thread: Writing and reading - Extrange issue

  1. #1
    Registered User Egalestrenge's Avatar
    Join Date
    Dec 2015
    Location
    Spain
    Posts
    14

    Writing and reading - Extrange issue

    Hello, my name is Egal and I'd like to post here a serious (but simple, I think) doubt about this simple writing & reading code.

    The idea is quite easy: I create the void routine write() that writes the value of a double "number" in a new file called "document.dat". Then I create another void routine, read(), which reads the writed value and prints it by console.

    Inside main() I invoke both routines directly, one after the other, and I expect to obtain a value of 0.500000.

    Code:
    /*  File:   main.c
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <stdbool.h>
    #include <string.h>
    
    
    void write(){
        
            FILE *file1;
            double number = 0.5;
            
            file1 = fopen("document.dat","w+");
            fprintf(file1,"%f\n",number);
            fclose(file1);
            
        }
    
    
    void read(char filepath[]){
        
        FILE *file;
        char buffer[0x8];
        double r;
        
        file = fopen(filepath,"r+");
        
        fseek(file, 0x0, SEEK_SET);
        fread(buffer, strlen(buffer), 1, file);
        sscanf(buffer, "%lf", &r);
    
    
        
        if(r!=0.000000){
        printf("Value: %f\n", r);}
        
        fclose(file);
        
        
    }
    
    
    
    
    int main() {
        
        write();
        read("document.dat");
        
        /*FILE *file;
        char buffer[0x8];
        double r;
        
        file = fopen("document.dat","r+");
        
        fseek(file, 0x0, SEEK_SET);
        fread(buffer, strlen(buffer), 1, file);
        sscanf(buffer, "%lf", &r);
    
    
        
        if(r!=0.000000){
        printf("Value: %f\n", r);}
        
        fclose(file);*/
        
        
        
    
    
        return (EXIT_SUCCESS);
    }

    The thing is, when I compile (using the linux gcc compiler) just like you can see in the code, the terminal prints 0.000000. But if I try reading only the recentrly created document.dat file without invoking write(), the console prints 0.50000 correctly. This means that write() is creating the file correctly and there must be an error between write() and read() execution. This is weird, since both are independent and, if I'm not mistaken, read() cannot be executed if write() execution hasn't been completed (and hence, the file has been closed and ready to be read).

    But just in case, I've modified the code, and write the read() rotuine explicitly inside main():

    Code:
    /*  File:   main.c
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <stdbool.h>
    #include <string.h>
    
    void write(){
        
            FILE *file1;
            double number = 0.5;
            
            file1 = fopen("document.dat","w+");
            fprintf(file1,"%f\n",number);
            fclose(file1);
            
        }
    
    
    void read(char filepath[]){
        
        FILE *file;
        char buffer[0x8];
        double r;
        
        file = fopen(filepath,"r+");
        
        fseek(file, 0x0, SEEK_SET);
        fread(buffer, strlen(buffer), 1, file);
        sscanf(buffer, "%lf", &r);
    
    
        
        if(r!=0.000000){
        printf("Value: %f\n", r);}
        
        fclose(file);
        
        
    }
    
    
    
    
    int main() {
        
        write();
        //read("document.dat");
        
        FILE *file;
        char buffer[0x8];
        double r;
        
        file = fopen("document.dat","r+");
        
        fseek(file, 0x0, SEEK_SET);
        fread(buffer, strlen(buffer), 1, file);
        sscanf(buffer, "%lf", &r);
    
    
        
        if(r!=0.000000){
        printf("Value: %f\n", r);}
        
        fclose(file);
        
        
    
    
        return (EXIT_SUCCESS);
    }
    and surprisingly it works (prints 0.500000).

    Funny thing is, if I compile in MAC with it's corresponding gcc compiler, the first code works fine too.

    Any idea about why is this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    fread(buffer, strlen(buffer), 1, file);
    Here's why:
    • fread is meant for binary stream input, i.e., it is typically used to read what was written with fwrite. You wrote with fprintf, which is meant for text stream output. If you want to use fprintf to read, then you should use a text stream input function to read, e.g., fscanf, or fgets with sscanf.
    • In all your examples, you did not initialise buffer, so strlen(buffer) results in undefined behaviour.
    • The second parameter of fread is for the size of one element of the type to be read, i.e., if you want to use fread, you should have written:
      Code:
      fread(&r, sizeof(r), 1, file);
      The corresponding fwrite would have been something like:
      Code:
      fwrite(&number, sizeof(number), 1, file1);
      However, note that in this case you should open the file in binary mode, even though it might not matter for the platforms that you are using.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Egalestrenge's Avatar
    Join Date
    Dec 2015
    Location
    Spain
    Posts
    14
    Thank you, now works fine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An issue with writing to an Integer Array
    By sam.briggs in forum C Programming
    Replies: 10
    Last Post: 01-26-2012, 11:41 PM
  2. issue reading in text
    By dford425 in forum C Programming
    Replies: 2
    Last Post: 04-25-2011, 09:24 PM
  3. Reading File Issue
    By dr0be in forum C Programming
    Replies: 23
    Last Post: 05-06-2007, 09:41 AM
  4. Reading and Writing
    By niroopan in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2002, 08:58 PM
  5. Reading and Writing in C++
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2002, 03:47 PM