Reading from a file

This is a discussion on Reading from a file within the C Programming forums, part of the General Programming Boards category; I have a file with names: Tammy Jason Carl Thomas Henry I created a character array name and then have ...

  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 09:21 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    ((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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    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 10: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    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, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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