Scanning(copying) a .txt file to struct array

This is a discussion on Scanning(copying) a .txt file to struct array within the C Programming forums, part of the General Programming Boards category; As the title says , i am trying to scan a .txt file which includes characters and integers and copy ...

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

    Scanning(copying) a .txt file to struct array

    As the title says , i am trying to scan a .txt file which includes characters and integers and copy that to a struct array ,
    file is like this :

    100201 asdsad das 40 50 70
    100202 fdgdf hsdhdfs 30 90 75

    like this.it goes , "ID Name Surname grade1 grade2 final"
    i used fscanf with %[^\n] but it just copied rest of the line.
    i couldnt even manage to assign the scanned values to variables by fgetc()

    any help?
    i managed to do that:


    Code:
    main (){
            int c;
            struct student{
                   long id;
                   char name[15];
                   char surname[15];
                   int mid1;
                   int mid2;
                   int final;
                   int score;};
            struct student x[10];
        
            
            
            FILE *file;
            file=fopen("grades.txt","r");
            if(file==NULL){
        		printf("Can not open the file\n"); 
        		exit(-1);}
        	
            
       //i ll add for cycle after i managed to scan  for (int i=0;i<10;i++)
            
            fscanf(file,"%d %[^\n] %[^\n]s %d %d %d", &x[0].id,&x[0].name,&x[0].surname,&x[0].mid1,&x[0].mid2,&x[0].final);
            
            system ("pause");	
        	return 0;	
                }
    
    /*i also tried this 
    c=fgetc(file); 
        while(c!=' '){ 
        fputc(c,x[0].id);
        }*/

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,369
    What exactly is the problem you are having?
    Notice that you are missing the "s" after the first "%[^\n]"
    It appears that you don't need the "[^\n]" for this application. When I removed both of them and corrected the "%s" it seemed to read the first line correctly for me.
    Please be more explicit in what you are expecting and what you are actually getting.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by cagurtay View Post
    i used fscanf with %[^\n] but it just copied rest of the line.
    That's what it's supposed to do. it reads into a string until it finds a newline.
    To read a long use "%ld".
    You don't need any spaces in the format because whitespaces is skipped by default.Also you don't need to specially treat '\n' because that is whitespace as well.
    To read a string use "%s".
    For strings you don't have to pass the address of the char * ( e.g. use x[0].name )
    Try again and post your updated code if it still doesn't work
    Kurt

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    AH! I was using %c instead of %s .
    Thank you!
    Code:
    for (int i=0;i<10;i++){
        fscanf(file,"%d %s %s %d %d %d\n",&x[i].id,&x[i].name,&x[i].surname,&x[i].mid1,&x[i].mid2,&x[i].final);
        printf ("%d\n",x[i].id);
        printf ("%s\n",x[i].name);
        printf ("%s\n",x[i].surname);
        printf ("%d\n",x[i].mid1);
        printf ("%d\n",x[i].mid2);
        printf ("%d\n",x[i].final);
    }

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    fscanf(file,"%d %s %s %d %d %d\n",&x[i].id,&x[i].name,&x[i].surname,&x[i].mid1,&x[i].mid2,&x[i].final);
    You should not use & for the char arrays here. The "name" and "surname" fields are already char arrays and as such their use in this context is taken to be the address of the first element of said arrays. Therefore they are already effectively addresses/pointers as used and do not need a & in front of them. It should simply be:
    Code:
    fscanf(file,"%d %s %s %d %d %d\n",&x[i].id,x[i].name,x[i].surname,&x[i].mid1,&x[i].mid2,&x[i].final);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Thank you but i have another problem now, i am trying to use that struct in a function that i made to sort the numbers but command-line is crashing...

    this is my function :
    Code:
    void bubsort(struct student x[10]){
         int z,y,temp=0;
         char temp2[15];
         for(z=0; z<10; z++){
             for(y=0; y<9; y++){
                     if(x[y].mid1>x[y+1].mid1){
                     
    				temp = x[y+1].mid1;
                    x[y+1].mid1 = x[y].mid1;
                    x[y].mid1 = temp;
                    
                    strcpy (temp2,x[y+1].name);
                    strcpy (x[y+1].name,x[y].name);
                    strcpy (x[y+1].name,temp2);
                    
                    strcpy (temp2,x[y+1].surname);
                    strcpy (x[y+1].surname,x[y].surname);
                    strcpy (x[y+1].surname,temp2);
                    
                    //temp2 = x[y+1].name;
                    //x[y+1].name = x[y].name;
                    //x[y].name = temp2;
                    //temp2 = x[y+1].surname;
                    //x[y+1].surname = x[y].surname;
                    //x[y].surname = temp2;
                               } } } }
    I tried to use the // parts first but its giving me error that i cant assign an array.
    and this is how i call the function:

    Code:
    printf ("Choose sorting type,for midterm1=1 , midterm2=2, final=3.\n");
        scanf ("%d",sort);
        if (sort==1)
           bubsort(x);
           for (int k=0;i<10;i++)
               printf("%s %s %d",x[i].name,x[i].surname,x[i].mid1);

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    I suggest that you declare a function:
    Code:
    void student_swap(struct student *x, struct student *y);
    Implement this function to swap two struct student objects. Test that the swapping works. If it does, use this function to help you implement bubble sort.
    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

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Quote Originally Posted by laserlight View Post
    I suggest that you declare a function:
    Code:
    void student_swap(struct student *x, struct student *y);
    Implement this function to swap two struct student objects. Test that the swapping works. If it does, use this function to help you implement bubble sort.
    I am sorry but i didnt understand what am i suppose to do.
    Am i need to declare new "student y" and make a function
    Code:
    void student_swap(struct student *x, struct student *y);
    to swap what?

    i can sort int , but my problem is i dont know how to declare a temp for storing string.
    Last edited by cagurtay; 08-08-2012 at 01:15 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    Quote Originally Posted by cagurtay
    I am sorry but i didnt understand what am i suppose to do.
    Am i need to declare new "student y" and make a function
    Code:
    void student_swap(struct student *x, struct student *y);
    to swap what?
    To swap any two struct student objects. You would pass a pointer to each object to this function.

    Quote Originally Posted by cagurtay
    i can sort int , but my problem is i dont know how to declare a temp for storing string.
    You would declare it as a local variable in the student_swap function.
    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

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Code:
    void student_swap(struct student *x){
         int z,y,temp;
         char temp2[10];
         for(z=0; z<10; z++)
         for(y=0; y<9; y++){
                  if(x[y].mid1>x[y+1].mid1){
             temp = x[y+1].mid1;
             x[y+1].mid1 = x[y].mid1;
             x[y].mid1 = temp;
             strcpy (temp2,x[y+1].name);
             strcpy (x[y+1].name,x[y].name);
             strcpy (x[y+1].name,temp2);
             strcpy (temp2,x[y+1].surname);
             strcpy (x[y+1].surname,x[y].surname);
             strcpy (x[y+1].surname,temp2);
                              }}}
    This somewhat working , it sorts the grades but doesnt match the names with the grades.
    İnput :
    ali durmus 40
    anil erdiz 30
    levent dogan 60
    can akkurt 45
    asli devecioglu 65
    sevde pir 70
    ecem bektas 75
    idil saracoglu 55
    mehmet umur 72
    murat hot 73
    Output =
    murat hot 75
    mehmet umur 73
    idil saracoglu 72
    ecem bektas 70
    sevde pir 65
    asli devecioglu 60
    can akkurt 55
    levent dogan 45
    anil erdiz 40
    ali durmus 30

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Just a little hint: In contrast to arrays you can assign to structs:
    Code:
    #include <stdio.h>
     
    struct test
    {
        int a;
        char *b;
    };
     
    int main(void)
    {
        struct test a = {1, "AAAA"};
        struct test b;
        
        b = a;
        printf("a: %d %s\n", a.a, a.b);
        printf("b: %d %s\n", b.a, b.b);
    
        return 0;
    }
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanning txt file values into an array
    By skmightymouse in forum C Programming
    Replies: 16
    Last Post: 04-28-2012, 01:38 PM
  2. Saving Struct to Binary File/Copying Structs
    By timmeh in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2009, 08:44 PM
  3. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  4. Copying an array string into a struct
    By JFonseka in forum C Programming
    Replies: 2
    Last Post: 02-27-2008, 05:24 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

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