File I/O

This is a discussion on File I/O within the C Programming forums, part of the General Programming Boards category; I have a program that uses a simple text file for data. When the program runs, I want it to ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    File I/O

    I have a program that uses a simple text file for data.

    When the program runs, I want it to check and see if the data file is present in the same folder. If not, I want to create it. If the data file already exists I don't want the program overwriting the existing file.

    In Qbasic I did this by opening the file as Append, immediately closing it and then opening the file as Input.

    When attempting to do this in C, I get a compile error where I attempt to close the "open as Append" command.

    I'm certain the open/close command is correct, because I'm able to write data to the file after opening it as Append (and this also removes the compile error).

    I assume this ain't nuthin' but a C thing. I don't want to do anything to the file when I open it as Append, so how can I close it quickly and quietly without modifying the file?

    Or is there a better way to do this?

    Thanks!

    mw

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    This program will give you the idea.

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	FILE *fp;
    	
    	if (argc!=2)
    	{
    		printf("USAGE: %s <file name>\n",argv[0]);
    		exit(0);
    	}
    
    	if ((fp=fopen(argv[1],"ra")) == NULL)
    	{
    		printf("File Does not exists. Creating one...");
    		fp=fopen(argv[1],"w");
    		if (fp!=NULL)
    		{
    			printf("successful.\n");
    			fclose(fp);
    		} else {
    			printf("failed.\n");
    		}
    	} else {
    		printf("File exists.\n");
    		fclose(fp);
    	}
    	
    	return 0;
    }
    Sample Output
    $ ./file1.exe demo.txt
    File Does not exists. Creating one...successful.

    $ ./file1.exe demo.txt
    File exists.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    207
    It doesn't seem to work on my compiler. I use MSVC++ ver 6.0 at work, and it gives me this output:

    USAGE: C:\Documents and Settings\GameQuest\My Documents\Blucast\Debug\file_initialization.exe <file name>Press any key to continue.

    "Blucast" is the folder I use to store my programs. I assume it's trying to create the file in the same folder, but it's unable to.

    Do you know why it's not working? This is more advanced than I'm used to yet... :-(

    mw
    Last edited by Lionmane; 07-01-2005 at 11:18 AM.

  4. #4
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25
    This is a command-line program looking for arguements. Hitting Run in the MSVC environment won't give it any arguements by default (maybe someone else knows a way to do that).

    You can open a cmd.exe console window and go to:
    C:\Documents and Settings\GameQuest\My Documents\Blucast\Debug

    Then type:
    file_initialization data.txt

    Or in the code, you can replace argv[1] with a string of your own making if you want the filename to be static. Then Run in MSVC will work if you get rid of the argc check.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Correct me if i'm wrong ..

    1. you give the file name.
    2. the file is opened if it exists in the same folder.
    or
    it will create a new file.

    in that case what you can do is

    open the file in read mode.
    fp=fopen(<filename>,"r");

    filename you can read it from the keyboard.

    what happends is, if the file already exisist then the file will be opend and the pointer is assigned with the address of the file.

    if the file doesn't then the NULL be returned..

    check fp for NULL. if thats the case then open the file in write mode. i.e instead of "r" use "w" . This will create a new file.

  6. #6
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25
    Yeah Machoscorpion, that's probably how I'd do it too. For just checking if the file exists, I think read-append and just plain read will have the exact same effect, but since we have no intention of appending anything, I'd open with just "r" as you described.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    207
    Thanks guys!

    mw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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