Thread: something about scanf() and FILE readed

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    something about scanf() and FILE readed

    **if the origin FILE opened has following strings:
    10,20,30,40,50,60,70
    **I want to make them to an array x[n],and n is the amout number of the
    FILE,as the example ,n is 7(n is not a const. so i CAN'T declare the correct
    length of array before)
    **But, my code only can get the first number 10, for when it encount ",",it
    was terminated,what should I do?(help me please~)
    Code:
    /*=======================*/
    int x[100];
    i=0;
    int chk,count;
    do{
         count=0;
         
        chk =fscanf(fp,"%d",&x[i]);
        i++;count++} while(chk!=NULL);
    int n=count;
    Last edited by 鄧名傑; 11-25-2012 at 07:56 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I would suggest you to check fgets . The link comes with an example.

    In standard C you can not learn the length of your data if it nos fixed.
    (I mean you have to read the data to find out)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try
    Code:
        chk =fscanf(fp,"%d",&x[i]);
        fgetc(fp);  // burn a comma, newline or other single character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Thanks you,Salem , std10093^^ ,I am the beginner of C ,could you give me more detailed code for how should I do?

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You checked the example from fgets? What did you not understand?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    I have modified my code ,could you help me debug it,THX >_<
    Code:
    /*=======================*/
    int x[100];
    i=0;
    int chk,count;
    char c;
    
    
    do{
         count=0;
        c = fgetc (fp);
         x[i]=atoi(c);
    
          if (c == ',') i++;    } while (c != 'EOF');  
    
    int n=count;

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    But you have not opened the file? Or you have done it and you did not post it?

    c has to be an int because of EOF handling.As a result atoi has to be removed.

    EOF is defined . You must remove the quotes.

    Check this code. Read the comments.
    Code:
    /*=======================*/
    #include <stdio.h>
    
    int main(void)
    {
        FILE* fp;
        /* I named the txt file test.txt */
        fp=fopen("test.txt","r");
        int x[100];
        int i=0;
        int count = 0;
    
        /* Read from file until EOF is reached */
        while(fscanf(fp,"%d",&x[i])!=EOF) {
             i++;    /* Increment by one the index of the array , */
             /* so that you do not overwrite what you just read   */
    
             /* Eat a comma , as salem said */
             fgetc(fp);
    
             /* Count how many numbers you read */
             count++;
        }
    
        for(i=0 ; i<count ; i++)
             printf("%d\n",x[i]); 
    
        /* Do not forget to close the stream */
        fclose(fp);
    
        return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of using both i and count in the loop that reads the input, you might as well just use count since the current count will always be the index of the next element to be read into. Also, you should check that count < 100 before reading into x[count].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    Instead of using both i and count in the loop that reads the input, you might as well just use count since the current count will always be the index of the next element to be read into. Also, you should check that count < 100 before reading into x[count].
    I totally agree with laserlight. I was based on the user's code and did not think about it. Thank you.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Thanks you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with scanf given a file as input
    By lower level in forum C Programming
    Replies: 1
    Last Post: 04-11-2011, 12:03 AM
  2. Possible bug in scanf()?
    By Wolf` in forum C Programming
    Replies: 13
    Last Post: 03-02-2011, 08:55 AM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. File I/O, fgets()? scanf()? I'm lost...
    By Lau in forum C Programming
    Replies: 26
    Last Post: 11-25-2002, 09:49 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM