Like Tree1Likes
  • 1 Post By laserlight

something about scanf() and FILE readed

This is a discussion on something about scanf() and FILE readed within the C Programming forums, part of the General Programming Boards category; **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 ...

  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
    32,823
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    22,265
    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].
    AndiPersti likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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, 03: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, 05:34 AM

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