Thread: read txt file with strings and integers

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    16

    read txt file with strings and integers

    i have prepared a code the read from txt file with values such integers and strings. but the code i have prepared reads only 1 line. how can i make the code to read multiple records from txt file.

    input records example in txt file:
    9009 James 90

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    
     
    
    
    void main()
    {
          int c;
            struct student{
                   long id;
                   char name[20];
                    int mid1;
      };
          
            
            struct student x[20];
             
            FILE *file;
             int i;
             i=0;
            
            file=fopen("student.txt","r");
            system("cls");
            if (fscanf(file,"%d %s %d\n",&x[i].id,x[i].name,&x[i].mid1) != NULL) {
             printf ("%d %s %d\n",x[i].id,x[i].name,x[i].mid1);
            }
            
            
    fclose(file);
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
            if (fscanf(file,"%d %s %d\n",&x[i].id,x[i].name,&x[i].mid1) != NULL) {
             printf ("%d %s %d\n",x[i].id,x[i].name,x[i].mid1);
    
    // change the above to this:
    
       int  i=0;
       while((fscanf(file, "%ld %s %d\n",&x[i].id,x[i].name,&x[i].mid1)) == 3) {
          printf("%ld %s %d\n",x[i].id,x[i].name,x[i].mid1);
          ++i;
       }
    fscanf(), can be quite fussy, but the idea is that it returns the number of items it has scanned and stored into a variable. When it can't get three items, it should have reached the very end of the file.
    Last edited by Adak; 11-16-2013 at 02:26 AM.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    thank you so much its working perfectly i have working on it for days.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    i am surprised you answered Adak, especially seeing this was posted 3 hours after the last part of "C Prog. Read .txt file with strings and integers"

    lol

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He needed to see that while loop, I believe.

    O.T.:
    Did you catch the Anand Vis....vs. Magnus Carlsen chess match earlier? Champ Anand lost for the second time in a row!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Prog. Read .txt file with strings and integers
    By akosinoah in forum C Programming
    Replies: 7
    Last Post: 11-16-2013, 12:30 PM
  2. scanning strings and integers from a file
    By GaelanM in forum C Programming
    Replies: 15
    Last Post: 03-31-2011, 09:38 PM
  3. read unknown integers from text file
    By underlink in forum C Programming
    Replies: 2
    Last Post: 11-10-2009, 10:23 AM
  4. Replies: 9
    Last Post: 03-17-2006, 12:44 PM
  5. how to read integers from a file( novice )
    By Gorgorath in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2002, 04:25 AM