Thread: Reading in data from Text file

  1. #166
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    i have a loop to read in from the text file
    Code:
    char place[11][11]
         {
    	       for(i=0;i<11;i++)
    	       {
    	     	fscanf(in_file, "&#37;s ", &place[i]);
    	        }
    i need this constructor to get the data from the place array or re read from text file i cant have it set how it is below
    Code:
    const char *cities[] = {"London", "Glasgow", "Birmingham", "Manchester", "Luton", "Derby", "Poole", "Stafford", "Stoke", "Cardiff"};

  2. #167
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    fscanf(in_file, "%s ", &place[i]);
    remove the &.

    If you add a
    Code:
    printf("'%s'\n", place[i]);
    do you get the right city names?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #168
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Mats, Patience not deniable. Statement made pertaining to over all of posts. Talented - yes, a little abrasive on some responses - yes. Please understand this is not an attack. It's a matter of seeking improvement on information transfer. It was something that needed to be said. Others on this forum would have loved to present what was said, but they do not post in fear of possible reduction in information. Please note that it goes without saying that there are allot of members on this board that do appreciate the contributions that you, Elysia, and all others who know who they are provide. All members on this forum should receive positive direction at all times.

  4. #169
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    Code:
    fscanf(in_file, "%s ", &place[i]);
    remove the &.

    If you add a
    Code:
    printf("'%s'\n", place[i]);
    do you get the right city names?

    --
    Mats
    yea when i call to print out it works fyne i use this to print out the palces array
    Code:
       for (row=0; row<11;row++) 
            {
                printf("%-11s",place[row]);
            }
    sooo all the correct place names are sotred in the array i just need to get them into the constructor somehow

  5. #170
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post

    sooo all the correct place names are sotred in the array i just need to get them into the constructor somehow
    What "conctructor"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #171
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    What "conctructor"?

    --
    Mats
    right i have this statically set
    Code:
    const char *cities[] = {"London", "Glasgow", "Birmingham",	"Manchester", "Luton", "Poole", "Stafford", "Stoke", "Cardiff", "Derby"};
    i need it to either get the data from the text file or from the places array
    Last edited by fortune2k; 04-04-2008 at 09:51 AM.

  7. #172
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what exactly are you doing to try to achieve that - if your places array is correctly read then you can replace your cities constant with the places array.

    Of course, the places array only needs to be 10 elements long, not 11 - but a spare slot at the end will not hurt anything in a modern machine - it's only another 11 bytes after all. But to be pedantic, the array declaration should be:
    Code:
    char place[10][11]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #173
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    So, what exactly are you doing to try to achieve that - if your places array is correctly read then you can replace your cities constant with the places array.

    Of course, the places array only needs to be 10 elements long, not 11 - but a spare slot at the end will not hurt anything in a modern machine - it's only another 11 bytes after all. But to be pedantic, the array declaration should be:
    Code:
    char place[10][11]

    --
    Mats
    hmmm yes but i use the constructor for determing input

    i use it for
    Code:
              for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) 
                                {
                                   if(strcmp(cities[x], pos[i])==0)
                                   {
                                   point[i] = x;
                                   flag=1;
                                   }
                                }
                                if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }

    i need the place names in the constructor but i can set them statically so i have to get them from text file or places array

  9. #174
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, now I'm quite confused about what you are asking for.

    What I was trying to say is that you should be able to do:
    Code:
              for(x = 0; x < sizeof(place) / sizeof(place[0]); x ++) 
                                {
                                   if(strcmp(place[x], pos[i])==0)
                                   {
                                   point[i] = x;
                                   flag=1;
                                   }
                                }
              if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }
    I also updated your indentation to match the code-flow.

    Of course, with the above change, the constant cities array is no longer needed [at least not here, and you should be able to replace it similarly in all other places]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #175
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    So, now I'm quite confused about what you are asking for.

    What I was trying to say is that you should be able to do:
    Code:
              for(x = 0; x < sizeof(place) / sizeof(place[0]); x ++) 
                                {
                                   if(strcmp(place[x], pos[i])==0)
                                   {
                                   point[i] = x;
                                   flag=1;
                                   }
                                }
              if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }
    I also updated your indentation to match the code-flow.

    Of course, with the above change, the constant cities array is no longer needed [at least not here, and you should be able to replace it similarly in all other places]

    --
    Mats


    thankyou i shall see if this fixes my problem

  11. #176
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right i have replaced cities with place and the validation is ok if i put a invalid place in it asks for re entry
    however the distances are incorrect
    http://i57.photobucket.com/albums/g2...Untitled-1.jpg


    the place names in the txt file : ( the - is included as a place name to but doesnt need to be searched for )
    Code:
     -     London	Glasgow	Birmingham	Manchester	Luton	Derby	Poole	Stafford	Stoke	Cardiff
    Last edited by fortune2k; 04-04-2008 at 10:28 AM.

  12. #177
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's probably easiest to just skip the first item on the cities row, by adding a dummy fscanf() before the for-loop that reads the city names.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #178
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    It's probably easiest to just skip the first item on the cities row, by adding a dummy fscanf() before the for-loop that reads the city names.

    --
    Mats
    what do you mean by that ?

  14. #179
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post
    what do you mean by that ?
    That you add code to read the "no-city" marker at the first position before you read the 10 city names.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #180
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    i think ive cracked it still testing this is what ive got
    Code:
         for(x = 0; x < sizeof(place) / sizeof(*place); x ++) 
                                {
                                   if(strcmp(place[x+1], pos[i])==0)
                                   {
                                   point[i] = x;
                                   flag=1;
                                   }
                                }
                                if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }

    x+1 pushes the array up 1 so it forgets bout -

    sorry for poor formatting

    what you think outputs seem good ect

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 02:30 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM