Thread: Storing Info From file into Array of structs

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    28

    Storing Info From file into Array of structs

    Hello,

    I have started to write a program that uses structs, however I am unsure of how to get the data correctly into the array of structs. This is what I Have so far...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct caneType Cane;
     struct caneType{
       char name[20];
       int lattitude;
       int longitude;
       float force;
      }hurricane;
     
         
    int main (int agrc, char *argv[]){
    char *str = *++argv;
    FILE *fp, *fp2;
    fp = fopen(str, "r");
    fp2 = fopen(str, "r");
    Cane storm[1000];
    int i,input = 0;
    
    
    
    	do{
    	fscanf((fp2,"%s %i %i %f", &name, &lattitude, &longitude,&force) != EOF);
    	storm[i]=input;
    	i++;
    	}
    	while(i<1000);
    	
    	return 0;
    }
    The input line will look like:
    Code:
    Alberto 80 8 42.720000
    Im getting slightly confused on how to read this data from my input file and storing it into my array of structs.

    Thanks for any suggestions...

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> fscanf((fp2,"%s %i %i %f", &name, &lattitude, &longitude,&force) != EOF);

    The syntax is wrong, there. The stuff in the first set of parenthesis gets passed to fscanf, and then you should test the return value of fscanf to make sure that it equals 4 (the number of variables being scanned). If not, return an error. Also, instead of using a hard-coded count of 1000, you could simply break out of the loop if fscanf returns EOF (or if the counter reaches the array size(1000), whichever comes first). And to prevent buffer overflow use a width specifier in the fscanf call for the string argument (ie: %19s).

    >> storm[i]=input;

    Well, you can't, of course, assign a struct to an integer. Copy the values scanned to the structure, or else pass the structure member directly to fscanf.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    ok...Ive changed it up. But when it comes to copying the scanned values into the structure, I'm having some trouble with the syntax.

    Code:
    int j=0;
    	while((i=fscanf(fp2,"%10s %i %i %f", &name, &lattitude, &longitude,&force))!=EOF){
    	
    		/*storm[j] = name;*/
    		storm[j].lattitude;
    		storm[j].longitude;
    		storm[j].force;
    		j++;
    		}
    	
    	
    	return 0;
    }
    the storm[j].lattitude etc... gives me a "statement with no effect" error...I'm not very familiar with structs and I've hit google and have been going thru the book but cant seem to get what i need...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well. What do you want do with storm[j].latitude? Or storm[j].longitude? Etc. Were you planning to assign them the values of latitude, longitude, etc? Then do so.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    I want to assign the values that were read in from the input file and store them into the array of structs. If my input was:

    Alberto 80 8 42.720000
    Alex 18 64 106.800000


    I want to store the values into their respective place within this structure:

    Code:
    typedef struct caneType Cane;
     struct caneType{
       char name[20];
       int lattitude;
       int longitude;
       float force;
      }hurricane;

    I am using a while loop, but I am having some trouble getting the proper syntax to correctly store the input values in the struct.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Generally you use = to assign values. (The exception of course is strings, where you use strcpy.)
    Last edited by tabstop; 06-25-2009 at 07:30 PM.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    oh thx. Is there a different way that we must handle char, and strings? the integers are fine, but when I attempt to store the "char name" with:

    Code:
        while((i=fscanf(fp2,"%10s %i %i %f", &name, &lattitude, &longitude,&force))!=EOF){
    	
    		storm[j].name = name;
    I get an incompatible data types error...what am i doing wrong?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "The exception of course is strings, where you use strcpy."

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Do I hear an echo in here?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    lol...there was one for a minute, i was trying to figure out how to use strcpy to copy the input to my struct. But im drawing blanks..here is what I have so far...


    Code:
    for(g=0;g<1000; g++){
    		strcpy(storm[g].name);
    		
    		}
    Am I shaking the right trees with this one?

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Am I shaking the right trees with this one?

    I'm guessing you didn't look up the documentation for strcpy, then, correct? That should always be the first step before attempting to use a function. In any case, it would require pointers to two arrays, at the very least.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    yeah...im still having some trouble with this strcpy. I guess I need to do some more reading on how to use it, and then figure how to implement it with the struct...

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> yeah...im still having some trouble with this strcpy. I guess I need to do some more reading on how to use it, and then figure how to implement it with the struct...

    Take a look at this link and see if it doesn't make sense to you.

    EDIT:
    As a matter of fact, you should learn *all* of the C runtime library functions. It'll make things much easier on you in the long run.
    Last edited by Sebastiani; 06-25-2009 at 09:10 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    alright its seems to make more sense so I tried this:

    Code:
    /*This reads from the input file, and store the data into the array of structs*/
    
    while((i=fscanf(fp2,"%10s %i %i %f", name, &lattitude, &longitude,&force))!=EOF){
    	
    		for(g=0;g<1000; g++){
    		strcpy(storm[g].name,name);
    		
    		}
    		storm[j].lattitude = lattitude ;
    		storm[j].longitude = longitude;
    		storm[j].force = force;
    		j++;
    I dont get any errors when compiling. so thats a start. storm[g].name is my destination within my struct, but my source is an input file, so that makes me think my second argument is wrong. sorry...Dont mean to seem so basic...

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why are you copying the name 1000 times?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read a file (binary) into an array of structs
    By Separ in forum C Programming
    Replies: 3
    Last Post: 04-14-2009, 09:09 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread