Creating files

This is a discussion on Creating files within the C Programming forums, part of the General Programming Boards category; Hi all, I've just learned how to create files in C, and I have an entire program written for an ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    23

    Creating files

    Hi all,

    I've just learned how to create files in C, and I have an entire program written for an inventory. The code compiles and runs, the problem is that the file that I have created won't open within the program. The function prototype that initializes the file is below:
    Code:
    void initialize(void)
    {
    	int i;
    
    	struct inventory blanklist = { 0, "", 0, 0.0};
    
    	FILE *cfptr;
    
    	if((cfptr = fopen("harware.dat", "wb+")) == NULL){
    		printf("File could not be found");
    	}
    	else{
    		for(i = 1; i <= 100; i++){
    			fwrite(&blanklist, sizeof(struct inventory), 1, cfptr);
    		}
    	}
    }
    So my questions is, what is the best way to create a file, then use it in the program itself?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you mean by "won't open within the program"? If you mean you get the "file could not be found" thing, that's very strange as the file should be created if it doesn't already exist.

    Now, you do need to fclose your file at the end of this function -- file I/O is pretty much always buffered, and so it's not going to get written (at all) to the file itself until you close the file (or do a flush, I suppose). Then another function can fopen it, etc.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Now, you do need to fclose your file at the end of this function -- file I/O is pretty much always buffered, and so it's not going to get written (at all) to the file itself until you close the file (or do a flush, I suppose).
    Ouch! Didn't know that...then again I usually do fclose() just to stay in style.
    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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    Ouch! Didn't know that...then again I usually do fclose() just to stay in style.
    Obviously if your program returns normally, all the buffers are flushed. It's when you want to write to the file and then read that back in the same program where you have to be careful.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    23
    OK, I used the following line to close it in the function.

    Code:
    fclose(cfptr);
    When I ran the program, I still got the message "File could not be found".

    I do try to reopen it within the main code of the program, but it isn't working.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you get the file could not be found message here, or where you try to open it elsewhere? Did you spell it "correctly" ("hardware.dat") the other time?

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    23
    God, I can't believe a typo was causing me so much grief! Thanks for spotting that! It's working now. Thanks again for your help.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by MK27 View Post
    Ouch! Didn't know that...then again I usually do fclose() just to stay in style.
    Well, any sane C library will close any open FILE objects during termination, even if you didn't do it yourself. But in theory, yes -- if you don't fclose() a file object the data may not be written to the file correctly.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  2. creating multiple files
    By lime in forum C Programming
    Replies: 8
    Last Post: 01-01-2004, 01:55 PM
  3. Creating text files
    By Crash1322 in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2003, 08:55 PM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 08:35 AM

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