Thread: problem updated

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    problem updated

    Hi,

    After I posted to the question, I have do some searching on delimiter.

    Try to modify the program, it almost solved the problem.

    Only the number 256 problem remain unresolved.

    Can someone please help.

    Thanks.
    Henry

    Code:
    #include <stdio.h>
     
    int main()
    {
        FILE *fp=fopen("e:\\text.txt", "r");
     
     
        char city[256],country[256];
        int  pop;
     
        while(!feof(fp))
        {
            fscanf(fp, "%[^%s%s%d]", city,country, &pop);
            printf("%s%s%d",city,country, pop);
        }
     
        fclose(fp);
     
        getchar();
        return 0;
    }
    What I want to do is to
    1. Get city name from user
    2. If is in file, print its population.

    If is do able, do I have to
    ...............1. Copy the population input to tmp[]
    ...............2. Clear out the country and population input
    ...............3. Compare char by char to see the input city name is in the file before , in each line.
    ...............4. If found
    .......................4.1 put back tmp[] into buffer
    ....................... assume
    ..................................(A) country[] removed from buffer &
    ..................................(B) the buffer now contain only two input info, (A) city name[] (B) populati.

    ......................4.2 Print the cityname and its population.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by hen_ry View Post
    Hi,

    After I posted to the question, I have do some searching on delimiter.

    Try to modify the program, it almost solved the problem.

    Only the number 256 problem remain unresolved.

    Can someone please help.

    Thanks.
    Henry

    Code:
    #include <stdio.h>
     
    int main()
    {
        FILE *fp=fopen("e:\\text.txt", "r");
     
     
        char city[256],country[256];
        int  pop;
     
        while(!feof(fp))
        {
            fscanf(fp, "%[^%s%s%d]", city,country, &pop);
            printf("%s%s%d",city,country, pop);
        }
     
        fclose(fp);
     
        getchar();
        return 0;
    }
    What I want to do is to
    1. Get city name from user
    2. If is in file, print its population.

    If is do able, do I have to
    ...............1. Copy the population input to tmp[]
    ...............2. Clear out the country and population input
    ...............3. Compare char by char to see the input city name is in the file before , in each line.
    ...............4. If found
    .......................4.1 put back tmp[] into buffer
    ....................... assume
    ..................................(A) country[] removed from buffer &
    ..................................(B) the buffer now contain only two input info, (A) city name[] (B) populati.

    ......................4.2 Print the cityname and its population.
    Where is your scanf() to get the city's name from the user?

    You need to really study up on the basics, here. You don't need to "remove" anything. Just print out the strings that you need to print out, and don't print out the strings you don't want to have printed.
    Last edited by Adak; 05-07-2009 at 11:43 AM.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Hi Adak

    Problem solved, thanks for your help.

    Henry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM