Thread: Simple file processig problem

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    35

    Question Simple file processig problem

    I have a text file in which the data is like this:
    SHANGHAI 13831900
    ISTANBUL 13120596
    KARACHI 12991000
    DELHI 12565901
    ...
    It goes on like this with city names and populations. I want to have such a code that it will assign every city and population to a certain element of the array A[] after it reads the file. That is, then I want to have this:
    A[0]=SHANGHAI
    A[1]=13831900
    A[2]=ISTANBUL
    A[3]=13120596
    ...
    I could only do the reading file part like this:
    Code:
      FILE * fPtr;
      fPtr = fopen("POPULATIONS.TXT", "r");
    What should I do next, please could you help?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Use fgets to read each line of text. Use sscanf to read from the line you just read or use strtok to tokenize it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And I doubt you want one array that has both words and numbers in it. Maybe you want an array of structs? Or perhaps two parallel arrays, one of words and one of numbers?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by tabstop View Post
    And I doubt you want one array that has both words and numbers in it. Maybe you want an array of structs? Or perhaps two parallel arrays, one of words and one of numbers?
    Actually I want an array of structs . I want to have one like this:
    Code:
    struct City
    {
    char CityName[50];
    double Population;
    };
    But I don't know how to do it. How can I assign every city and population to the array of struct from the file?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by farukyaz View Post
    But I don't know how to do it. How can I assign every city and population to the array of struct from the file?
    By reading them in, using any of your favorite reading-from-file functions (fscanf or fgets). If you know that none of your cities will contain a space, you can use the slightly-easier fscanf, otherwise as claudiu says you should read a line with fgets then split it up.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    I'll go with fscanf. Thanks a lot!!!

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by farukyaz View Post
    I have a text file in which the data is like this:
    SHANGHAI 13831900
    ISTANBUL 13120596
    KARACHI 12991000
    DELHI 12565901
    ...
    It goes on like this with city names and populations. I want to have such a code that it will assign every city and population to a certain element of the array A[] after it reads the file. That is, then I want to have this:
    A[0]=SHANGHAI
    A[1]=13831900
    A[2]=ISTANBUL
    A[3]=13120596
    ...
    I could only do the reading file part like this:
    Code:
      FILE * fPtr;
      fPtr = fopen("POPULATIONS.TXT", "r");
    What should I do next, please could you help?
    The very next thing you should do in situations like this is consult your compiler and library documentation and try to find suitable commands... The web is the last place you should try.

    After problem analysis, the second most important skill a programmer can have is "looking stuff up".

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by CommonTater View Post
    The very next thing you should do in situations like this is consult your compiler and library documentation and try to find suitable commands... The web is the last place you should try.

    After problem analysis, the second most important skill a programmer can have is "looking stuff up".
    Yes, but after being directed from here too I found fscanf the most suitable command, but I just don't know how to use it and I couldn't find enough information on the internet either so that's the exact point I need help in..

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by farukyaz View Post
    Yes, but after being directed from here too I found fscanf the most suitable command, but I just don't know how to use it and I couldn't find enough information on the internet either so that's the exact point I need help in..
    1) Since there are differences in implementation between various compilers you need to get a copy of your compiler's specific documentation to use as a reference text. If you can't find it... find a compiler that IS documented. (Personally I think Pelles C has the best help file I've ever seen)

    2) scanf - C++ Reference <-- click the red text.

    3) Given your file sample, I'd try something like...

    Code:
    struct City
    { char CityName[50];
      int Population;    
     } CityRec;
    
    
    // then in your input loop
    
    fscanf("%49s %d", CityRec.CityName, &CityRec.Population);
    If you backtrack that into the fscanf() and scanf() documentation you should be able to see how it woks...
    Last edited by CommonTater; 05-14-2011 at 12:56 PM.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by CommonTater View Post
    1) Since there are differences in implementation between various compilers you need to get a copy of your compiler's specific documentation to use as a reference text. If you can't find it... find a compiler that IS documented. (Personally I think Pelles C has the best help file I've ever seen)

    2) scanf - C++ Reference <-- click the red text.

    3) Given your file sample, I'd try something like...

    Code:
    struct City
    { char CityName[50];
      int Population;    
     } CityRec;
    
    
    // then in your input loop
    
    fscanf("%49s %d", CityRec.CityName, &CityRec.Population);
    If you backtrack that into the fscanf() and scanf() documentation you should be able to see how it woks...
    The link was helpful, thanks. And your code seems like what I want, if I can figure out the for loop I will use it. Here I have 81 cities and 81 corresponding populations in my POPULATIONS.TXT file. So I tried sth like this;
    Code:
    for(i=1; i<=81; i++)
    Now I need to put the fscanf under the loop but how can I do it since there is not a loop counter "i" in fscanf? All I've read about were a few fcanf commands without a need for a loop.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It seems to me like you have no idea how to use arrays whatsoever, otherwise it should become clear that "i" will be the index of your structure array.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by claudiu View Post
    It seems to me like you have no idea how to use arrays whatsoever, otherwise it should become clear that "i" will be the index of your structure array.
    I know arrays very well but I wasn't sure about an array of struct, and because my compiler still gives a lot of errors I had to ask it.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First if you are getting errors you don't understand, no, you don't know arrays very well at all...

    Based on my previus example...

    Code:
    typedef struct City
    { char CityName[50];
       int Population;    
     };
    
    // create the array...
    int CitiesSize = 10;
    City Cities = malloc(CitiesSize * sizeof(City));
    
    // open your file
    CityFile = fopen(filename,"r");
    
    // initialize a counter
    int CityCount = 0;
    // read your file
    while ( fscanf(CityFile, "%49s %d", Cities[CityCount].CityName, &Cities[CityCount].Population))
      { CityCount++;
    
         // manage the array
         if (CityCount > CitiesSize)
           {CitiesSize += 10;
             Cities = realloc( Cities,CitiesSize * sizeof(City); } }
    
    // close the file
    fclose(CityFile);
    
    printf("%d Cities loaded\n",CityCount);
    That will read the file, no matter how many cities, up to the limit of memory.

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Thanks a lot! I understand all you suggested.
    By the way yes I know arrays very well, as long as it's only array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem with simple file program
    By lautarox in forum C++ Programming
    Replies: 10
    Last Post: 09-08-2008, 10:31 AM
  3. File I/O:simple code problem
    By Ardetcho in forum C Programming
    Replies: 4
    Last Post: 07-18-2006, 10:32 AM
  4. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  5. Simple End of file problem
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 03-04-2002, 01:05 PM

Tags for this Thread