another file handling problem

This is a discussion on another file handling problem within the C Programming forums, part of the General Programming Boards category; this seems like the working code to create a file if it isnt already created, and open it likewise: Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    another file handling problem

    this seems like the working code to create a file if it isnt already created, and open it likewise:

    Code:
            if(!auto_file)
                auto_file = fopen(path, "a+");
            else
                auto_file = fopen(path, "a+");
        
        fprintf(auto_file, "hello4");
        fgets(test, 250, auto_file);
        printf(test);
    and the output in the created/opened file is:



    Code:
    hello4=
    i don't know if it shows up on the forum, but there is a lot of whitespaces when i didnt put them in. and also the "=", where did that come from???

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    both fopen lines are identical.

    how is variable test declared? Open the file with Notepad.exe or some other text editor to see what it shows. I'll bet that white space is not really in the data file.
    Last edited by Ancient Dragon; 04-26-2006 at 12:00 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    i dont know why i put the same line twice, but when i do open the file with a text editor that output is exactly what i get, with all those white spaces. the above result isnt the program output, its the file output

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Show us how you declare/initialize the test array.

    Also, Ancient Dragon's point about your if/else construct was a good one. Your program does the same thing regardless of whether the test is true or false. Why perform the test at all?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I think the problem is that you do not rewind the file after writing to it and reading it back. fgets() will read the file from current file position up to either the first 0 is encountered or the buffer has been filled up. Here is an example of how to make it work right.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char* argv[])
    {
    	char path[] = "myfile.txt";
    	char test[255] = {0};
    	FILE* auto_file = fopen(path, "a+");
    	if(auto_file == 0)
    	{
    		auto_file = fopen(path,"w");
    		fclose(auto_file);
    		auto_file = fopen(path, "a+");
    	}
    	fprintf(auto_file, "hello4");
    	rewind(auto_file);
    	fgets(test, 250, auto_file);
    	printf(test);	
    	fclose(auto_file);	
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > printf(test);
    Understand that this is very dangerous.
    If there are any % characters, printf will try and interpret them, and there are no parameters!

    Use fputs() or fputs() for writing strings, or at the very least printf("%s",string);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  4. Problem while dealing with file handling functions
    By RoshanGautam in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 01:42 AM
  5. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 03:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21