Thread: How to assign a word/number to a variable

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    Question How to assign a word/number to a variable

    Hi,
    You might remember me from the post I posted last time about text files... Well, this time I start from from scratch, this program That I need to do is reading a file(text) and sorting/searching. My question is "How can I assign words/numbers that are in the text file into a variable?" that way I can sort/search.
    Any suggestions?

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Thats a very broad quiestion. There are many ways to read data from a file, well maybe not too many but...depending on what way you read your data will determine how you assign your information to a variable. If your using fscanf as you read the file you are assigning words, numbers to your variables. If you use fgets or something along those lines you'll have to break your information down after you've read each line to assign your variables. fgets is probably the best way to do it, although Im sure some would argue, but fscanf would be the simplest way as long as your not assigning more than one word to a variable(that is possible though). i.e. fscanf(file, "%s", fullname); When full name is John Smith it would only recognise John and throw a space in your next imput and throw smith in the one after that(I think).

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    Thumbs up

    Thanks, GravtyKlz. I will try your idea and try to see if it works.
    If anyone else would like to post another possible solution go ahead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  3. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. The syntax to declaire a variable in a function.
    By loopy in forum C Programming
    Replies: 9
    Last Post: 05-25-2002, 06:17 AM