Thread: scanning strings and integers from a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
      int i = 0, j, index[i];
      char first_name[999][22];
    
      while(fscanf(data_file, "%s", first_name[i])) {
         i++;    //welcome to C! :)     = i + 1;
        index[i] = i;  //you're building an index array at the same time? Good!
      }
      //i has just counted the number of names in the array, so it can be used:
      --i; //step it back one time
      for (j = 0; j < i; j++) {  //start with 0 not 1
        printf("%s, ", first_name[j]);
      }
    Last edited by Adak; 03-31-2011 at 05:20 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    hmm, i gave it a shot but i just get segmentation faults. i also tried playing around with the loop and the size of the array just to see where the program was trying to call on something it couldnt but no dice

    why does my professor hate us so much

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you change the data in the file, so it was just first names?

    You can't just ask the computer program to scan for one thing, and not give it code to handle the rest of the data - it's all read in SEQUENCE.

    Try it with a data file of just a few first names, in a different data file, and change the name of the file your program is opening.

    Alex
    Henrik
    Jonathan
    Anze
    Jarome
    Patrick
    Brad
    Teemu
    Sidney

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    oooooooh i see!

    now if i i had a data file like our TA's are going to use with mutliple columns of data, how exactly would i make the scan work around this?

    sorry for asking so many questions, we reeeeeeeally werent taught anything about dealing with these kinds of problems so im at a complete loss right now. i definitely want to keep taking classes for this in school but this is driving me mental lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings from file
    By N3M3SiS in forum C Programming
    Replies: 3
    Last Post: 01-17-2011, 06:42 PM
  2. Read max value from strings of numbers in text file
    By james890 in forum C++ Programming
    Replies: 14
    Last Post: 04-15-2010, 03:26 PM
  3. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  4. Reading Lines from File Issue (integers and character strings)
    By kbfirebreather in forum C Programming
    Replies: 4
    Last Post: 10-17-2009, 02:02 PM
  5. Replies: 9
    Last Post: 03-17-2006, 12:44 PM