Thread: basic file handling problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    basic file handling problem

    Hello,

    I've got an assignment where the first section involves asking the user for the name of the file to open, if the file does not exist, then a file should be created. I thought that i had acheived this but it still doesnt function the way i want to. If the file doesn't exist, it doesn't seem to go in the IF loop where it should print the 'file not found' message. Can someone give me some guidance please.

    Code:
    // Barcode1.cpp : Defines the entry point for the console application.
    
    #include <stdio.h>
                  
    
    int main()
    {
    
    	FILE *fin, *fout;
    
        char barfile[100];
    
    	printf("Please enter the name of the file you wish to open..\n");
    	scanf("%s", &barfile);
    
    	fin = fopen(barfile, "a");
    
    	if (fin==NULL) {
    		printf("File has not been found. File will be created.\n");
    		  fout=fopen(barfile,"a"); 
    		  }
    
    	printf("Start scan now, press any key to stop scan..\n");
    
    	{
    	
    	
    		
    	}
    
    }

    thanks
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely, you only want to either open or create ONE file, not two different files?

    And "w" may be a better option for creating than "a" ("a" means append, which assumes existing file, I think).

    Edit: "a" will create a file if it doesn't exist, so you will only get a NULL back if the file CAN NOT BE CREATED (e.g. contains invalid characters, the disk is full).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    ah ok i see what you mean. thanks for the reply, i'll change the code and see how it goes.

    thanks
    george

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    hello, ive worked on it somemore, but im still finding trouble doing the file handling, i still dont have a full grasp on it. i think my logic is right how: if it doesnt find the file, its goes to the if(NULL) statement, then creates a file. However, the problem i have now is that it does not concatenate '.txt' at the end of the newly created file. i got the strcat working if i added it after the very first fin command, but now when it creates the file, it doesnt add the .txt extension on the end. could i get some more guidance please.

    thanks in advance
    george

    Code:
    // Barcode1.cpp : Defines the entry point for the console application.
    
    #include <stdio.h>             
    #include <conio.h>
    #include <string.h>
    
    
    int main()
    {
    
    	FILE *fin;
    
        char barfile[100];
    	char ext[5] = {'.','t','x','t'};
    
    
    	printf("Please enter the name of the file you wish to open..\n");
    	scanf("%c", &barfile);
    
    	fin = fopen(barfile, "w");
    //	strcat(barfile,ext);
    	//printf(" %c", barfile);
    
    	if (fin==NULL) {
    
    		printf("File has not been found. File will be created.\n");
    		fin=fopen(barfile,"a"); 
    		strcat(barfile,ext);
    
    }
    
    //	fin=fopen(barfile,"w"); 
    	//strcat(barfile,ext);
    
    	printf("Start scan now, press any key to stop scan..\n");
    
    	while(1)
        {
    
    
            if(kbhit())
            {
                printf("Scanning stopping...\n");
    			break;
            }
        }
    
    	printf("To start another scan, press 1..\n");
    
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by georgen1 View Post
    hello, ive worked on it somemore, but im still finding trouble doing the file handling, i still dont have a full grasp on it. i think my logic is right how: if it doesnt find the file, its goes to the if(NULL) statement, then creates a file. However, the problem i have now is that it does not concatenate '.txt' at the end of the newly created file. i got the strcat working if i added it after the very first fin command, but now when it creates the file, it doesnt add the .txt extension on the end. could i get some more guidance please.

    thanks in advance
    george

    Code:
    // Barcode1.cpp : Defines the entry point for the console application.
    
    #include <stdio.h>             
    #include <conio.h>
    #include <string.h>
    
    
    int main()
    {
    
    	FILE *fin;
    
        char barfile[100];
    	char ext[5] = {'.','t','x','t'};
    
    
    	printf("Please enter the name of the file you wish to open..\n");
    	scanf("%c", &barfile);
    
    	fin = fopen(barfile, "w");
    //	strcat(barfile,ext);
    	//printf(" %c", barfile);
    
    	if (fin==NULL) {
    
    		printf("File has not been found. File will be created.\n");
    		fin=fopen(barfile,"a"); 
    		strcat(barfile,ext);
    
    }
    
    //	fin=fopen(barfile,"w"); 
    	//strcat(barfile,ext);
    
    	printf("Start scan now, press any key to stop scan..\n");
    
    	while(1)
        {
    
    
            if(kbhit())
            {
                printf("Scanning stopping...\n");
    			break;
            }
        }
    
    	printf("To start another scan, press 1..\n");
    
    }
    You're not checking if the file exists, you're just opening it, before any ".txt" is appended onto the filename. (In red).

    The message in blue should be: file could not be created. It has nothing to do with it not being found, since you never looked for it. Check out findfirst and findnext, or other commands for ways to find a file.

    Please, use just one fopen(), not two or three different one's. If you want your data appended, then use the append mode. It's the same as write mode, if the file didn't exist, previously.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 02:26 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM