Thread: Problem with reading of file

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    9

    Problem with reading of file

    Hi all,
    I am generating a value by calling some fn which returns uint32_t and storing these values in file. Part of Code as shown below
    Code:
    //to write content in file
    uint32_t c;
    ofstream outfile;
    	outfile.open("output",ios::app|ios::out|ios::binary);
    // putting value in file using loop
    outfile<<c;
    //to read content of file
    ifstream myfile ("output");
    	myfile.is_open();
    	myfile>>num;  // for first record display
    printf("value fetched from file: %.8x\n", num); 
    
    int record = 2* sizeof(uint32_t);     //offset to reach third record
    	myfile.seekg( record, ios::beg);
    	myfile>>num1;
    	printf("value fetched from file: %.8x\n", num1); 
    	myfile.close();
    the above code gives some garble value. I tried the next code which gives me just the first value and with offset again gives garble values.
    Code:
    ofstream outfile;
    	outfile.open("output");
    outfile<<c<<endl;
    File "output" contains the value but i am not able to read the value, may be problem with the offset.
    pl help
    rgds

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It is probably a good idea to open your input file with binary if you wrote it with binary.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    9

    Unhappy

    Hi
    I have refined my code as under but no success so far
    Code:
    
    
    ifstream myfile ("output",ios::in|ios::binary);

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You should probably be using the read() and write() member functions when dealing with binary files, as there may not be any separation of the values ,

    Jim
    Last edited by jimblumberg; 05-28-2010 at 06:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM