Thread: Problem with reading/writing files

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Problem with reading/writing files

    Code:
    void add_person(Control *c)
    {
    	char line[1024];
    	char buff[1024];
    	if((file = fopen(filename,"r+")) == NULL)
    	{
    		error(app,"Could not open file main.add!");
    	}
    	fgets(buff,1024,file);
    	fputs(buff,file);
    
    	strcat(line,"<");
    	strcat(line,get_control_text(mainw.f_lname));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_fname));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_add));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_st));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_zip));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_home));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_work));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_cell));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_bd));
    	strcat(line,"|");
    	strcat(line,get_control_text(mainw.f_rel));
    	strcat(line,">");
    	ask_ok(app,"TEST",line);
    	fputs(line,file);
    	fclose(file);
    	clear_boxes();
    	free(buff);
    	free(line);
    }
    when I add one person, I get this "<|||||||||>", but when I add another, I get "<|||||||||><|||||||||><|||||||||>".

    Why?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    char line[1024];
    char buff[1024];
    You should add = { '\0' }; to the end of those two statements. Local variables are not guaranteed to be allocated to zero. Most likely, the old data is still there.

    Code:
    free(buff);
    free(line);
    You only use free when you malloc( ) something.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    so write:

    Code:
    char line[1024] = { '\0' };
    I'll try that (for both chars).

  4. #4
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    that didn't fix it >

    ... what if they were global?

  5. #5
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    ... is there a function that completely NULLs a string?

  6. #6
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    Originally posted by kinghajj
    ... is there a function that completely NULLs a string?
    memset(string,0,string_length) ;

  7. #7
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    still doesn't work...

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try outputting the contents of buff and of line to the screen, and see what shows.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    ... I solved it... but it doesn't make sence...

    I deleted this line:
    Code:
    fputs(buff,file);
    ... that shouldn't work... I thought that fputs overwrites what is currently in the file. I guess not

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by kinghajj
    I thought that fputs overwrites what is currently in the file. I guess not
    How data is sent to your file depends entirely on how you open the file.

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

  11. #11
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    how so?

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Problem with reading/writing files

    Your first strcat() should be strcpy(). This will erase the old contents of the buffer and start fresh. No need to erase the buffer before using then.

    Or, instead of making all those strcat() calls, how about:
    Code:
    sprintf(line,
            "<%s|%s|%s|%s|%s|%s|%s|%s|%s|%s>",
            get_control_text(mainw.f_lname),
            get_control_text(mainw.f_fname),
            get_control_text(mainw.f_add),
            get_control_text(mainw.f_st),
            get_control_text(mainw.f_zip),
            get_control_text(mainw.f_home),
            get_control_text(mainw.f_work),
            get_control_text(mainw.f_cell),
            get_control_text(mainw.f_bd),
            get_control_text(mainw.f_rel));
    Also, why are you doing an fgets() then immediately an fputs() to the file?
    The logic escapes me.
    Last edited by WaltP; 12-24-2003 at 12:18 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The main reason I can see is that you're reading and writing the same file.

    So open two files - one for reading, and another for writing.

    > strcat(line,"<");
    line is uninitialised, unless you initialise it like XSquared showed you.
    Another way is to do
    strcpy( line, "<" );
    and the rest can be strcat() calls
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  2. problem while opening files from multiple directories
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2004, 03:29 PM
  3. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  4. problem with resource files
    By nickeax in forum Windows Programming
    Replies: 1
    Last Post: 03-14-2003, 02:32 AM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM