Thread: need some help with writing and reading to/from a file

  1. #31
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by 999cm999 View Post
    I know I suck at C, but I'm not quite sure I see where I am doing the double read.

    In this block of code:

    Where is the double read coming from?
    Because you call fgets() in the while condition, then fread. The fgets() happens at each iteration, which advances the file pointer. This is the same issue you had with the text file read where I thot you were using sscanf and not fscanf. But fgets is pretty useless on binary data, because it reads up until a newline. '\n' has a byte value of 10. Here's a decimal dump (using the program from post #29) of hardware.dat after I entered "toaster 10 32666":

    Code:
    {
       1    0    0    0  116  111   97  115  116  101  | ****toaste
     114    0    0    0    0    0    0    0    0    0  | r*********
       0    0    0    0   10    0    0    0 -102  127  | ****/*****
       0    0  | **
    So: the first four bytes are the recordNum, which was 1, the bytes are in "little endian" order, ie, the least significant one first. For record #255, since the output is signed, those bytes would be "-1 0 0 0" (unsigned 255 = signed -1), for record #256, those bytes would be "0 1 0 0".

    Then you have the 20 bytes of toolname, which contains "toaster". Then you have the four bytes of "quantity", and since 10 fits into one byte, that's there (notice the / on the right).

    Which is where fgets would stop reading, leaving the file pointer for fread.

    The last four bytes are "cost". 32666 doesn't fit into one byte, and appears as "-102 127 0 0".

    So here's a textFile() that works. Compare it to what you have and consider why I changed what I changed:

    Code:
    void textFile( FILE *readPtr )
    {
    	FILE *writePtr;
    	struct hardwareData hardware = { 0, "", 0, 0 };
    
    	if ((writePtr = fopen( "hardware.txt", "w" )) == NULL )
    	{
    		printf( "File could not be opened.\n" );
    		return;
    	}
    	/* copy all records from random-access file to text file */
    
    	rewind(readPtr);
    	while (fread( &hardware, sizeof( struct hardwareData ), 1, readPtr)) {
    		/* write single record to text file */
    			fprintf(writePtr, "%d %s %d %d\n", hardware.recordNum, hardware.toolname, hardware.quantity, hardware.cost );
    			fprintf(stderr, "wrote #%d, %s\n", hardware.recordNum, hardware.toolname);
    	}
    	fclose( writePtr );
    
    	writePtr = fopen( "hardware.txt", "r" );
    
    	int toolNumber;
    	char toolName[20];
    	int toolQuanity;
    	int toolCost;
    	char buf[BUFSIZ];
     
    	while ( fgets(buf, sizeof(buf), writePtr) != NULL)
    	{
    		int check;
    		check = sscanf( buf, "%d%[^0-9-]%d%d", &toolNumber, toolName, &toolQuanity, &toolCost );
    		printf( "(check = %d) %d %s %d %d\n",check, toolNumber, toolName, toolQuanity, toolCost );
    	}
    	fclose( writePtr );
    }
    Last edited by MK27; 12-16-2011 at 03:47 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #32
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but I'm not quite sure I see where I am doing the double read.
    Both fgets() and fread() read information from a file. You do not want to use fgets() with a binary file, you only need fread().


    Jim

  3. #33
    Registered User
    Join Date
    Nov 2011
    Posts
    18
    Jim and MK, thank you both for all the help! I think I might finally be on my way to understanding C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  2. reading and writing to file
    By jrb47 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2006, 07:35 AM
  3. File reading and writing
    By Noobwaker in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 02:28 AM
  4. reading & writing to a file
    By Micko in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2004, 11:57 AM
  5. writing/reading txt file
    By Klinerr1 in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2004, 09:34 PM