Thread: trouble with text file reading/writing

  1. #1
    Registered User
    Join Date
    Aug 2006
    Location
    Hamilton, NZ
    Posts
    15

    trouble with text file reading/writing

    Hi everyone

    I have a text file which contains a memory dump as shown below

    Code:
      Memory Address 1FD7C000 49A0 Bytes
      1FD7C000: 4D 5A 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *MZ..............*
      1FD7C010: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C020: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C030: 00 00 00 00 00 00 00 00-00 00 00 00 B8 00 00 00  *................*
      1FD7C040: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C050: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C060: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C070: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C080: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C090: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C0A0: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  *................*
      1FD7C0B0: 00 00 00 00 00 00 00 00-50 45 00 00 4C 01 04 00  *........PE..L...*
    what i want to do is have my program read the above text file and output it into another processed file which will look like this

    Code:
    4D 5A 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 B8 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    
    and so on
    A friend at my previous work gave me a hand and we came up with this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <limits.h>
    
    int main()
    {
    
      char instring[200];
      int charpos;  // position of ":" in the string
      FILE *textfile,*outtxtfile ;
    
      charpos=10;
      // Process file
      textfile=fopen("dump.txt","r");
      // open the destination file
      outtxtfile=fopen("outtext.txt","w");
    
     // process file
      while (!feof(textfile))
    	{
    	fgets(instring,200,textfile); /* if ":" found in the string will confirm valid string &
    				     extract it to new file */
    	if(instring[10]==':')
    	   {
    	   // fwrite(void *buffer, size_t size, size_t num, FILE *fp);
    	   fwrite(instring+charpos+2,1,30,outtxtfile);  // first lot of data ie "45  5A  00  00  00 00 00 00"
    	   fwrite(instring+charpos+2+31,1,30,outtxtfile);  // second lot of 30 characters from "-" onwards
    	   }
    	}
      fclose(textfile);
      fclose(outtxtfile);
    
      return 0;
    }
    This program doesnt work how i would like it and returns the following

    Code:
    4D 5A 00 00 00 00 00 00-00 00 0 00 00 00 00 00  *MZ.........00 00 00 00 00 00 00 00-00 00 0 00 00 00 00 00  *...........00 00 00 00 00 00 00 00-00 00 0 00 00 00 00 00  *...........00 00 00 00 00 00 00 00-00 00 0 00 B8 00 00 00  *...........00 00 00 00 00 00 00 00-00 00 0 00 00 00 00 00  *...........00 00 00 00 00 00 00 00-00 00 0 00 00 00 00 00  *...........00 00 00 00 00 00 00 00-00 00 0 00 00 00 00 00  *...........00 00 00 00 00 00 00 00-00 00 0 00 00 00 00 00  *...........
    can anyone see where i am going wrong? and maybe come up with a way to fix it? sorry for the long post

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fgets( buf, BUFSIZ, fp ) != NULL )
    {
        if( (ptr = strstr( buf, ": " )) )
        {
            strncpy( output, ptr + 2, 48 );
            output[ 24 ] = ' ';
            output[ 48 ] = '\0';
            puts( output );
        }
    }
    Close enough for who it's for.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Location
    Hamilton, NZ
    Posts
    15
    Thanks for your reply! i really appreciate it

    what should i define ptr as and what is output? is that the outtxtfile fp?

    also i changed the first line to

    Code:
    while( fgets( instring, 200, textfile ) != NULL )
    is that right?
    Last edited by sbeehre; 08-01-2006 at 05:22 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is that right?
    Yes.
    The "why you shouldn't use feof() in a loop" is in the FAQ.
    Quzah's answer fixes that problem.

    Also, you might want to use
    sizeof(instring) rather than 200, so the code automatically tracks the declaration of your array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ptr is a pointer to a char, because that's what the function strstr returns.
    output would simply be another buffer, since you can't strncpy to a file...
    puts takes a pointer to a char as its argument, so that also could have clued you in as to what output is.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Location
    Hamilton, NZ
    Posts
    15
    Quote Originally Posted by quzah
    ptr is a pointer to a char, because that's what the function strstr returns.
    output would simply be another buffer, since you can't strncpy to a file...
    puts takes a pointer to a char as its argument, so that also could have clued you in as to what output is.

    Quzah.
    ok i have changed my program around with your suggestions and it almost works

    this is the new one

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <limits.h>
    #include <string.h>
    
    int main()
    {
    
      char instring[200];
      char output[200];
      int charpos;  // position of ":" in the string
      FILE *textfile,*outtxtfile ;
    
      charpos=10;
      // Process file
      textfile=fopen("dump.txt","r");
      // open the destination file
      outtxtfile=fopen("outtext.txt","w");
    
     // process file
    while( fgets( instring, sizeof(instring), textfile ) != NULL )
    {
        if( (charpos = strstr( instring, ": " )) )
        {
            strncpy( output, charpos + 1, 48 );
            output[ 24 ] = ' ';
            output[ 48 ] = '\0';
            puts( output );
        }
    }
      fclose(textfile);
      fclose(outtxtfile);
    
      return 0;
    }
    it outputs to the screen perfectly as shown below
    Code:
     4D 5A 00 00 00 00 00 00 00 00 00 00 00 00 00 00
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
     00 00 00 00 00 00 00 00 00 00 00 00 B8 00 00 00
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    How do i get it to write like that to a file? do i change puts( output ) to fputs ( output , outtxtfile) ?
    Last edited by sbeehre; 08-02-2006 at 12:46 AM.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Location
    Hamilton, NZ
    Posts
    15
    ok i tried fputs (output , outtxtfile) and it doesnt format correctly in the outout text file, its like its not putting end of line breaks in or something as the file is just one big long line.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Location
    Hamilton, NZ
    Posts
    15
    also one thing i didnt mention is that the file is actually over 1000 lines long.... the first piece of text was just the first few lines, will that make any difference in the running of the program? i didnt think it would so i didnt mention it before.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > its like its not putting end of line breaks in or something as the file is just one big long line.
    Put a newline character in the string then.

    > also one thing i didnt mention is that the file is actually over 1000 lines long.... will that make any difference in the running of the program
    No, nothing noticable.
    Last edited by whiteflags; 08-02-2006 at 01:32 AM.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Location
    Hamilton, NZ
    Posts
    15
    the \n in the string worked a treat... but when i use the dump.txt with 1179 lines in it i get nothing in the outtext.txt file, but with the small dump.txt it works perfectly.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Did you open the right files for reading and writing?

    Did you check if fopen returned a valid file pointer?

    Is there something about the big version of dump.txt that you're not telling us?

  12. #12
    Registered User
    Join Date
    Aug 2006
    Location
    Hamilton, NZ
    Posts
    15
    i know the im opening the right files because if i use the dump file in my first post it works perfectly. The big file is just more of the same as the small dump file and has 2 blank lines at the bottom.

  13. #13
    Registered User
    Join Date
    Aug 2006
    Location
    Hamilton, NZ
    Posts
    15
    anyone?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your code.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    i know the im opening the right files because if i use the dump file in my first post it works perfectly. The big file is just more of the same as the small dump file and has 2 blank lines at the bottom.
    That's not a very good 'know'. You should check fopen to see if it returns a NULL file handle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM