Thread: Reading names from a file into strings to reference later

  1. #1
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19

    Reading names from a file into strings to reference later

    I have a file of names, for example:
    Nick Greg Matthew

    I also have an array of:
    1 2 3

    Is there a way to represent Nick as 1, Greg as 2, and Matthew as 3 so that later down the road when let's say number 2 gets returned from a function, it can be referenced back to the string containing Greg and print out his name?

    I hope that's not too confusing/vague, it's only a small part of a larger picture :/

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's easy... just load the names from the file into an array... such as friends[100][30] then you can reference them as friends[x] in functions such as printf() and puts()

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Use a two-dimensional array. Like this.

    Code:
    char friends[100][3];
    This creates 300 bytes. 100 for Nick, 100 for Greg, and 100 for Matthew.

    Code:
    friends[100][0] = "Nick";
    friends[100][1] = "Greg";
    friends[100][2] = "Matthew";
    Use for loops with fscanf statements to obtain the strings from the file.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually Babcock... you have 100 strings each 2 characters long.

    The declaration I used creates 100 strings each 29 characters long and you access it as... friends[0] ... friends[99] using strcpy(), scanf() or fscanf() to put the names in the array.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Parallel arrays: one for strings[rows][columns], and the second one for all the data you want, data[row][columns].

    "Bob" can be found in strings[row] with strcmp(), and his index number will match the index number of his data in the data[row].

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by CommonTater View Post
    Actually Babcock... you have 100 strings each 2 characters long.

    The declaration I used creates 100 strings each 29 characters long and you access it as... friends[0] ... friends[99] using strcpy(), scanf() or fscanf() to put the names in the array.
    Oh, my bad.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Multiple Multi-Part Strings From a Text File
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2009, 10:43 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  4. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM