Thread: Reading from a file

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    47

    Angry Reading from a file

    I have a file with names:

    Tammy
    Jason
    Carl
    Thomas
    Henry

    I created a character array name

    and then have this:

    Code:
    while((currentCharacter = getc(fp)) != EOF)	
    	{	
    		fscanf(fp, "%s", name);
             
                    printf("%s \n", name);
    
    }
    My problem is my output for some odd reason cuts off the T on Tammy. All the other names print to the console correctly.

    How do i fix this?
    Last edited by drater; 02-14-2008 at 10:21 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    ((currentCharacter = getc(fp)) != EOF

    What do you think this does?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    Gets the current character as long as it isn't the end of the file.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Mhm, but you need to think of the consequences when it isn't the end of the file yet. Part of the data will be stored in the return value of getc(), including the T in Tommy and the whitespace that fscanf() puts back.

    As you might have guessed, scanf only can only change the stream state while some conversion is valid. Since string conversion ends when whitespace is encountered, *scanf() functions will put stuff back into the stream for future reading. Print out the character like this in your loop and see it's effects on your file reading:
    Code:
     printf("\"%c\"\n", currentCharacter);
    It's probably best to work with *one* input method.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    Code:
    printf("\"%c\"\n", currentCharacter);
    It printed 'T' at the top with '' for every other line. How do i make sure that it doesn't keep storing my T?

    Should I just "printf(%c\"\n", currentCharacter);" at the beginning of my loop? or is there a parameter i can put in my while loop to make sure it doesn't keep my T?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Now that you see that data is being stored where it shouldn't be, you can fix the problem by sticking to one input method. For instance, you could read a character at a time, or use fscanf()'s return value to control the loop as you read.

    See, the neat thing about fscanf is that it returns EOF also when there is nothing to read.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I tried:

    Code:
    while((currentCharacter = fscanf(fp, "%s", name)) != EOF)	// get next char from input file until we reach EOF
    And my program crashed. What am i doing it wrong?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    From the manual page.
    Code:
    RETURN VALUES
           These functions return the number of input items assigned,
           which can be fewer than provided for, or even zero, in the
           event  of  a matching failure.  Zero indicates that, while
           there was input available, no conversions  were  assigned;
           typically  this is due to an invalid input character, such
           as an alphabetic character for  a  `%d'  conversion.   The
           value  EOF  is  returned if an input failure occurs before
           any conversion such as an end-of-file occurs. If an  error
           or end-of-file occurs after conversion has begun, the num-
           ber of conversions which were  successfully  completed  is
           returned.
    My bad. But provided you understand what that says you should know what to do now. Just read until nothing is converted.

    This looks like

    while ( (currentCharacter = fscanf( /** ... **/ ) ) == 1 )

    currentCharacter now contains the sum of successful conversions.
    Last edited by whiteflags; 02-14-2008 at 11:56 PM.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    Thank you, citizen, for helping me with my original problem.


    I am supposed to tab if the name is more than 6 characters, and I know how to look for exactly 6 characters:
    Code:
    if ((int)cityName[6])
    but how do i do x > 6 length?

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by drater View Post
    Thank you, citizen, for helping me with my original problem.


    I am supposed to tab if the name is more than 6 characters, and I know how to look for exactly 6 characters:
    Code:
    if ((int)cityName[6])
    but how do i do x > 6 length?
    I'm not fully understand what you are trying to do, but strlen will return you number of characters in the null-terminated string

    so strlen(cityName) will give the number of chars in the cityName if it is null-terminated.

    PS. Bytheway if ((int)cityName[6]) - (int) here gives nothing to correctness of the code and makes it less readable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want to use fscanf, you had better read this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    Code:
    do
    {
    		
    	  fscanf(fp, "%s", name);
             
                      printf("%s \n", name);
    
    }while((currentCharacter = getc(fp)) != EOF);

    I think this will work for you.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And so you post the same poor use of fscanf even though I just posted a link about it.
    Read people!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM