Thread: Storing a String with values

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    10

    Storing a String with values

    Hi

    I am having problems with the following.

    I am trying to program a mileage chart that reads the names of places and miles from a text file

    Once read in will look similar to below.

    Hull Wall Stow Ford
    Hull 0 21 3 45
    Wall 21 0 5 65
    Stow 3 87 0 12
    Ford 45 65 12 0

    I can do this part and have got the chart displaying on screen by using the following code.

    Code:
    void showchart ()
    {
    int x, row, col;
    
    FILE *in_file;
    in_file = fopen("places.txt","r");
    
    for (x=0;x<=9; x++)
    {
    fscanf(in_file, "%4c", names[x]);
    }
    
    printf("\n     ");
    
    for (x=0; x<=9; x++)
    {
    printf("%6s",names[x]);
    }
    
    printf("\n");
    for (row=0; row<=9; row++)
    {
    printf("%s", names[row]);
    	for(col=0; col<=9; col++)
    {
    	fscanf(in_file, "%d", &grid[row][col]);
    	printf("%6d", grid[row][col]);
    	}
    printf("\n");
    }
    }
    Now I need to capture the names as a string and also the mile for each name, so I can sort the place names in alphabetical order and it will still display the correct mile in the grid when sorted.

    I have an idea of using the gets command to store the name and the miles in a temp array when it is read from the text file, but I am unsure how to do this.

    This is the code I have got so far and it doesnt work

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char names[10][5];
    void read_strings(char s[][5],int n)
    
    int main(void)
    
    {
    int x, i, s;
    
    FILE *in_file;
    in_file = fopen("places.txt","r");
    
    for (x=0;x<=9; x++)
    {
    fscanf(in_file, "%4c", names[x]);
    }
    
    for (x=0; x<=9; x++)
    {
    printf("%6s",names[x]);
    for (i=0; i<n; i++)
    {
    gets(char s[][i]);
    }
    }
    printf("\n");
    }
    }
    It prints the names on the screen (well it did before I entered the gets(char s[][i]); line) in a list but not the miles as I have not got round to this part yet.
    I am first trying just to get it to store the names so I can sort them, then I will try and add the code to store the miles with the names.

    I hope this makes sence?

    Thanks for any help.

    Or can any one suggest an easier way to do this?

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    I have an idea of using the gets command to store the name and the miles in a temp array when it is read from the text file, but I am unsure how to do this.
    Never use gets. LINK Replace it with fgets. LINK

    Just an idea but I am not sure if this is what you need. If it will always be the same locations you can use a structure.
    Code:
    struct location{
         int tohull, towall, tostow, toford;
    }
    Then you would have one of these structures and just fill it with info.
    Code:
    struct location Hull;
    Hull.tohull = 0;
    Then you just scan in the numbers with fgets and parse them into the structure.
    Code:
    char line[BUFSIZ], temp[50];
    fgets(line, sizeof(line), file);
    sscanf(line,"%s %d %d %d %d", temp, &Hull.tohull, &Hull.towall, &Hull.tostow, &Hull.toford);
    ~Sven
    Last edited by 00Sven; 05-09-2006 at 02:07 PM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your first link has an unnessesary "</br>" imbedded inside it.

    Code:
    gets(char s[][i]);
    That's not how you pass an argument to a function!

    Use something like this:
    Code:
    #include <string.h>
    /* ... */
    char s[1000], *p;
    /* ... */
    fgets(s, sizeof(s), stdin);
    if((p = strchr(s, '\n'))) *p = 0;
    /* now you have a string, s */
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  2. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM