Thread: Reading in data from Text file

  1. #196
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and I understand that you want this to be a function - but I don't understand what is IN your function, and what your call to the function looks like, and what exactly doesn't work when you use the function. That is the code I want posted - because that's the code you want to have working.

    But I think it will have to wait for later, as I'm off to bed now.

    --
    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.

  2. #197
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    Yes, and I understand that you want this to be a function - but I don't understand what is IN your function, and what your call to the function looks like, and what exactly doesn't work when you use the function. That is the code I want posted - because that's the code you want to have working.

    But I think it will have to wait for later, as I'm off to bed now.

    --
    Mats
    ok cheers for ur help up till now

    these are the varibles that are mainly involved

    char pos[2][16], = the use inputs a 2 place names via the loop
    char place[11][11], = stores all the place names which is gatherd via the read function
    int point[2] = holds the 2 points which corresponds with the place name

  3. #198
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post
    ok cheers for ur help up till now

    these are the varibles that are mainly involved

    char pos[2][16], = the use inputs a 2 place names via the loop
    char place[11][11], = stores all the place names which is gatherd via the read function
    int point[2] = holds the 2 points which corresponds with the place name
    Yes, but I need to see BOTH the function code and the code that calls the function - because if it's not working, then you are doing something wrong either in the calling of the function, or inside the function.

    It would also help if you describe what actually goes wrong (e.g. it doesn't find the right city).

    --
    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.

  4. #199
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    Yes, but I need to see BOTH the function code and the code that calls the function - because if it's not working, then you are doing something wrong either in the calling of the function, or inside the function.

    It would also help if you describe what actually goes wrong (e.g. it doesn't find the right city).

    --
    Mats

    ok ill start it off simple

    this is my function
    Code:
    void places()
    {
       char pos[2][16];
       char place[11][11];
       int point[2];
       int i, x, wrong, flag;
       for(i=0;i<2;i++) // loops round  twice allowing the input of 2 points
                {
                     if(wrong!=1)
                     {           
                        if(i==0){printf("\n Please enter your Start point: ");} // input first point 
                        if(i==1){printf("\n Please enter your Finishing point: ");} // input first point  
                        
                     }  
                        wrong=0;  
                        scanf("&#37;16s", pos[i]);
                        size_t x;  
                              flag = 0;
                              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;       
                                }
                }

    This is how i call it
    Code:
    places();
    and this is my output:

    http://i57.photobucket.com/albums/g2...Untitled-2.jpg

    it deosnt recognize any valid places which are input


    thats what i have for a starter

  5. #200
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
                              for(x = 0; x < sizeof(place) / sizeof(*place); x ++) 
                                {
                                   if(strcmp(place[x+1], pos[i])==0)
                                   {
                                   point[i] = x;
                                   flag=1;
                                   }
                                }
    place[x + 1] will cause a buffer overrun during the last iteration of that loop.

    But it doesn't matter, because you never read anything into your place[][] array. You need to put data into that array.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #201
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by dwks View Post
    Code:
                              for(x = 0; x < sizeof(place) / sizeof(*place); x ++) 
                                {
                                   if(strcmp(place[x+1], pos[i])==0)
                                   {
                                   point[i] = x;
                                   flag=1;
                                   }
                                }
    place[x + 1] will cause a buffer overrun during the last iteration of that loop.

    But it doesn't matter, because you never read anything into your place[][] array. You need to put data into that array.
    i have 11 items in my array i dont understand

  7. #202
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    i just need that for loop to work in the method i dunno what i need to add to it to get it working

  8. #203
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by fortune2k View Post
    i have 11 items in my array i dont understand
    When x = 10, you access element 10 (11th item in array) and x + 1 = 11 (12th item in array), but your array only contains 11 items, not 12.
    http://cpwiki.sf.net/Buffer_overrun
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #204
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    aghhhhgot yaaa instead od

    for(x = 0; x < sizeof(place) / sizeof(*place); x ++)


    shall i just change it to summin like x =0;x<10;x++

    will that achive the same thing? without an overrun

  10. #205
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    atm i aint really worried bout the buffer ect atm im trying to get this function working



    Quote Originally Posted by matsp View Post
    Yes, but I need to see BOTH the function code and the code that calls the function - because if it's not working, then you are doing something wrong either in the calling of the function, or inside the function.

    It would also help if you describe what actually goes wrong (e.g. it doesn't find the right city).

    --
    Mats

    ok ill start it off simple

    this is my function
    Code:
    void places()
    {
       char pos[2][16];
       char place[11][11];
       int point[2];
       int i, x, wrong, flag;
       for(i=0;i<2;i++) // loops round  twice allowing the input of 2 points
                {
                     if(wrong!=1)
                     {           
                        if(i==0){printf("\n Please enter your Start point: ");} // input first point 
                        if(i==1){printf("\n Please enter your Finishing point: ");} // input first point  
                        
                     }  
                        wrong=0;  
                        scanf("&#37;16s", pos[i]);
                        size_t x;  
                              flag = 0;
                              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;       
                                }
                }

    This is how i call it
    Code:
    places();
    and this is my output:

    http://i57.photobucket.com/albums/g2...Untitled-2.jpg

    it deosnt recognize any valid places which are input


    thats what i have for a starter

  11. #206
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by fortune2k View Post
    aghhhhgot yaaa instead od

    for(x = 0; x < sizeof(place) / sizeof(*place); x ++)


    shall i just change it to summin like x =0;x<10;x++

    will that achive the same thing? without an overrun
    No it doesn't. Read my reply again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #207
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    ive read it and well ill deal with that at the end ive been playing with my prog dont see any problems

    Quote Originally Posted by Elysia View Post
    No it doesn't. Read my reply again.

  13. #208
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    my main concern is figuring out how to get that piece of code into a fucntion so it runs all good and stuff when i call it to work all good so i dont have to have the same code in my program twice just call it twice

  14. #209
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    See the bold in my reply there? There's your problem. THINK!

    Quote Originally Posted by Elysia View Post
    When x = 10, you access element 10 (11th item in array) and x + 1 = 11 (12th item in array), but your array only contains 11 items, not 12.
    http://cpwiki.sf.net/Buffer_overrun
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #210
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       char pos[2][16];
       char place[11][11];
       int point[2];
    These variables are local to the function and uninitialized - I'm pretty sure you want them to be passed into the function.

    --
    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.

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