Thread: Basic File I/O

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    19

    Basic File I/O

    Hi guys! I'm pretty new to C and I'm a bit confused about the File I/O stuff.

    I'm trying to scan a word from a file. I can tell something is wrong with my code but I'm not sure whether it is the fscanf format or what.

    Code:
    char word; //word that will be read from the file
    
    
    	FILE *ifp = fopen( "myFile.txt", "r" ); //open myFile to read
    
    
    	if (ifp == NULL ) //if file doesn't exist
    	{
    		printf( "File does not exist\n" );
    
    
    		return -1; //return code
    	}
    
    
    	while (ifp == fopen( "myFile.txt", "r") != EOF)
    	{
    		fscanf(" %c", word);
    	}

    Also, if I want to scan a word from a file, would I use %c or %s ?

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You would (ideally) use fgets. But yes, a literal "word" (delimited by whitespace) could be read from a file pointer, using %s. If you wanted to read a line of text, with whitespace in it, you would have to do some tricks with %[] or simply use fgets() as I suggest.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    19
    I want the file to read just a word, not a sentence. So fscanf can't be used for this at all ?

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Your loop should be controlled by fscanf() or fgets(). You are calling fopen() in your loop repeatedly which makes no sense

    You want to read one singular word? If so then why are you running fscanf() in a loop? All you need is fopen(); fscanf(fp, "%s", string); fclose(); there you read 1 word.

    As I said of course you CAN use fscanf(), but the recommend way is to use fgets() and sscanf() to read out from the buffer whatever it is you want (one word per line, or whatever). That way you are processing the whole file instead of leaving garbage in the buffer for your next read, which is what fscanf() is gonna do.
    Last edited by nonpuz; 11-19-2013 at 08:56 PM. Reason: Fixed some formatting issues

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    19
    Ohh okay. SO something like

    fgets(str, 10, fp)

    would read a word from a file ? And no loop would be needed.

    EDIT:

    and if I wanted to print the word it read?
    for example:

    printf("The word is %s.", ________);

    What would be placed in the blank ?
    Last edited by GaitBait; 11-19-2013 at 09:09 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by GaitBait View Post
    Hi guys! I'm pretty new to C and I'm a bit confused about the File I/O stuff.

    I'm trying to scan a word from a file. I can tell something is wrong with my code but I'm not sure whether it is the fscanf format or what.

    Code:
    char word; //word that will be read from the file
    
    
    	FILE *ifp = fopen( "myFile.txt", "r" ); //open myFile to read
    
    
    	if (ifp == NULL ) //if file doesn't exist
    	{
    		printf( "File does not exist\n" );
    
    
    		return -1; //return code
    	}
    
    
    	while (ifp == fopen( "myFile.txt", "r") != EOF)
    	{
    		fscanf(" %c", word);
    	}

    Also, if I want to scan a word from a file, would I use %c or %s ?
    You see the problem with your while () loop? You would be trying to open a file, over and over, with each iteration, and fopen() never returns EOF, anyway, so it's an endless loop.

    fopen returns either an address, (pointer), or NULL, never EOF.

    Code:
    char word[50]; //word that will be read from the file
    
    FILE *ifp = fopen( "myFile.txt", "r" ); //open myFile to read
    
    if (ifp == NULL ) //if file doesn't exist
    {
    	printf( "File does not exist\n" );
    
    	return -1; //return code
    }
    
    
    while ((fscanf( "%s ",word)) > 0)
    {
    	printf(" %s", word);
    }
    The only problem with the above changes is that fscanf() is notoriously fickle, and likely to find something it doesn't like in the file being read.

    Whereupon it will quit!

    So changing this to use fgets() is a very worthwhile thing to do. The C Tutorial link at the top of the web page (near the center), will show you how to do that.
    Last edited by Adak; 11-19-2013 at 10:50 PM.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    19
    I understand now, thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic file handling - again!
    By stevendt in forum C Programming
    Replies: 32
    Last Post: 09-30-2011, 07:24 AM
  2. Basic file read, file write program
    By starwarsyeah in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 03:23 PM
  3. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  4. Very basic File I/O help
    By NewbGuy in forum C Programming
    Replies: 5
    Last Post: 08-02-2008, 05:32 PM
  5. Basic File I/O.... Can somebody help?
    By Spitball in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2004, 04:34 AM