Thread: Don't understand why this doesn't work. . .

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    Don't understand why this doesn't work. . .

    I'm basically wanting to read a file into a dynamically allocated array. This is the code I'm using to do this, but for some reason the do-while loop that uses fgets to read in the file line by line reads the last line twice for some reason, which gives me an erroneous file length. If anyone could explain why this is happening I would really appreciate it.

    Code:
    printf( "\n   Loading %s into memory. . .\n\n", argv[1] );
    	
    	char buffer[256];
    	int length = 0;
    	do {
    	   fgets( buffer, 256, in );
    	   length += strlen( buffer );
    	   
    	} while( !feof(in) );
    	
    	char *input_file = malloc( length );
    	strcpy( buffer, input_file );

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Simple answer.
    feof() returns true after an unsuccessful read.
    you could try
    Code:
            while (  fgets( buffer, 256, in )!= 0) {
    	   length += strlen( buffer );
    	}
    Kurt

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    strcpy parameters are destination then source, not source destination. IE:

    strcpy( input_file, buffer );

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    feof() is not a way to check if you should be reading. You should be checking the return value of fgets(), and if it returns NULL, then you can use feof() to tell if the read failed because of EOF.

    It should be something like this:

    Code:
    while((fgets(buffer, sizeof(buffer), in)) != NULL)
    {
        /* Do something with the data in buffer... */
    }
    Your code, however, is wrong. You're reading and reading and reading and you simply keep overwriting what you've just read. Then you dynamically allocate enough memory to read the entire thing, however, you call strcpy() on uninitialized memory!

    Code:
    char *input_file = malloc( length );
    This attempts to return a block of memory length bytes long, and has input_file point to it. This is well and good if it succeeds, but it might not, and input_file might be pointing to NULL. You should be checking for this.

    Code:
    strcpy( buffer, input_file );
    Totally wrong. You're trying to copy all the chars at the block of memory pointed to by input_buffer into buffer. Big mistake. You'll probably segfault at this point should you be copying more than 256 chars.

    Even if you do it the other way around, your buffer is pointless because it only holds the last 256 bytes read because you fail to save the previous 256 bytes read, and so on.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    Thanks for the replies. Sorry for the simple mistakes, I'm still learning.

    This may be totally off, but I was thinking something like this might work, but if I try to print the string pointed to by input_file, I get nothing. I need load the file into accessible memory locations so I can search through and print out various values at certain locations.

    Thanks again for helping.

    Code:
    printf( "\n   Loading %s into memory. . .\n\n", argv[1] );
    	
    	char buffer[256];
    	int length = 0;
    	while( fgets( buffer, sizeof(buffer), in ) != 0 ) {
    	   length += strlen( buffer );
    	}
    	
    	char *input_file;
    	if( (input_file = malloc( length ))  != NULL ) {	
    		fread( input_file, 1, 946, in );     //assuming input file is 946 bytes
    	}
    	
    	
    	printf( "input_file: %s\n", input_file );
    Last edited by k2712; 10-07-2007 at 03:26 PM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're reading it from the file with fgets() and then trying again to read with fread() after it runs out of data. The input is gone already. You've already read it.... What would you like the system to do? lol

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    I'm reading the file with fgets to determine the length of the input file. Once that is determined I want to copy each byte from the input file into the input_file array with fread. Obviously this isn't working and that is why I am asking for help.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    And I'm trying to explain to you that your method is 100% fundamentally incorrect. You need to stop wondering why something stupid isn't working.

    Let's try this again.

    You have a file. You have X number of bytes in the file. You read all X bytes. All X bytes are gone. You're at the end of the file. You then try to read the same bytes that are already read. Do you get it now? You're at the end of the file. There is nothing left to read....

    If you still don't get it, I can't help you much more than possibly to tell you to look up rewind(). Even that is probably not an ideal solution, but may work for what you want to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM