Thread: Altering / Recieving Information from text file question

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    Allen, Texas, United States
    Posts
    2

    Altering / Recieving Information from text file question

    Just to give some background so my question has some context...

    I'm reading in a text file containing the following :

    Name: John Smith
    Major: EE
    GPA: 3.85
    Name: Jlsd
    Major: CS
    GPA: 2.75

    And I need to print out to the console this :

    Name: GPA : Major:
    John Smith 3.85 EE
    Jlsd Sdfjkl 2.75 CS

    I'm not asking anyone to solve this , but could I get a nudge in the right direction ? Its pretty frustrating

    I know my train of thought right now is to get each individual line into an array element starting after the 'Name , GPA , and Major ' but I'm not exactly sure how to do that .

    Also , how exactly does a char array work? Does one element essentially hold a "string" or just one character of a string?

    example : exampleArray[0] = 'John Smith' ; is that valid and what would it hold? A 'J' or the full name?

    while( ( ch = fgetc(ptr)) != EOF )
    printf("%c",ch);

    I understand this prints out the whole file , but I'm not too sure how to alter this or how exactly to manipulate the gets() to only print or store stuff into another array.

    Thanks for your help! Any tips are welcome ! Keep in mind I'm a beginner programmer so go easy !

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The common method to process input is to use fgets() and sscanf() to parse the data. You will also want to lookup using structures
    as that is definitely what you would want to use here.

    As far as your question, a character array holds a single string, example:
    char array[20] = "John Smith";
    Creates an array of size 20 and initializes it with the string "John Smith"

    If you wanted to store multiple strings you would need multi-dimensional array or
    some other (more versatile) object (such as a linked list). Here you would probably want
    to represent each record with a structure, example:
    Code:
    struct student {
        char name[MAXNAMELEN+1];
        char major[3]; /* 2 char max limit? +1 for trailing \0 */
        float gpa;
    };
    Read up on the subjects I discussed above and you will be able to implement what you want.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For anyone but a raw beginner, records are stored in a struct, with struct members being what we call, record fields (like name, etc.).

    For you, parallel arrays are the way to go - you learn these before you learn structs. So you'll use three arrays: 1 char array for names[row][col], and another float array for the gpa, and a third array (again, char), for their major[row][col].

    And major[row][col] will hold one char, only. But names[row], since it has both rows AND columns, (a so called 2D array), will refer to an entire name, since it is a pointer to the first char in names[row][col].

    The way the info will be organized is very similar to what you were thinking about - one index will have one students data:

    names[0][], gpa[0], and major[0][] will all have data on John Smith, student #0.

    names[1][], gpa[1], and major[1][] will have data for student #1

    You will help yourself if you run through the tutorial linked to the C Tutorial tab, at the top of this page. Run through a few basics, at least.

    You're not ready for structs yet, but you will need the most basic input and output, to write your program.
    Last edited by Adak; 12-02-2012 at 01:11 AM.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Location
    Allen, Texas, United States
    Posts
    2
    Thanks a ton! I'm giong through the C tutorial tab now and finding it really helpful!

    Now , is there anyway to manipulate fgetsc() to get specific words? Or leave out certain words? So far in class we've mainly been working with standard print / scan file methods so I assume we need to use those but they don't seem specific enough for how I need to manipulate what I need from the file.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by hearthledger View Post
    Thanks a ton! I'm giong through the C tutorial tab now and finding it really helpful!

    Now , is there anyway to manipulate fgetsc() to get specific words? Or leave out certain words? So far in class we've mainly been working with standard print / scan file methods so I assume we need to use those but they don't seem specific enough for how I need to manipulate what I need from the file.
    fgets() is indeed easier initially, but then you still need to get your data into usable variables, probably using sscanf() anway. fscanf() works - it's just more of a pain to set up.

    If it's anything the user is entering for data, best to use fgets() since it can be tested more thoroughly. scanf(), fscanf() sscanf(), are all for strictly FORMATTED data. They don't deal well with ANY data that is not formatted to their specifications!

    I'm not familiar with fgetsc(), but yes, you can get specific words, but that's probably not what you want.

    I'll post an example, in a bit.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    With this text file:
    Code:
    Name: Able Anderson
    Major: BIO
    GPA: 3.4
    Name: John Smith
    Major: EE
    GPA: 3.85
    Name: Charles Green 
    Major: CS
    GPA: 2.75
    I'd suggest:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void) {
       int i;
       char names[3][40], major[3][30];
       char colon[3]={6,7,5}; //offset to the colon in the string of text+2
       char buffer[40];
       float gpa[3];
    
       FILE *fp=fopen("test.txt","r");
       if(!fp) {
          printf("Error! File did not open.\n");
          return 1;
       }
       i=0;
       while(fgets(buffer, sizeof(buffer), fp)!=NULL) {
          //printf("buffer:%s\n",buffer);
          sscanf(buffer+colon[0], "%[^\n]",names[i]);
          //get major
          fgets(buffer, sizeof(buffer), fp);
          sscanf(buffer+colon[1], "%[^\n]",major[i]);
          //get gpa
          fgets(buffer, sizeof(buffer), fp);
          sscanf(buffer+colon[2], "%f",&gpa[i]);
          printf("%s\n%s\n%.2f\n\n",names[i],major[i],gpa[i]);
          ++i;
       }   
       
       fclose(fp);
       printf("\n");
       return 0;
    }
    I tried a few other ideas, but they either were more of an intermediate type, or they were too complex or long.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about accessing record information from a file
    By TexasKid in forum C Programming
    Replies: 5
    Last Post: 04-23-2012, 10:26 AM
  2. Need help with altering info on a txt file
    By Netflyer in forum C Programming
    Replies: 29
    Last Post: 02-14-2008, 07:23 AM
  3. Text file question
    By RoD in forum Tech Board
    Replies: 13
    Last Post: 02-03-2005, 03:31 PM
  4. Replies: 10
    Last Post: 06-12-2002, 03:15 PM
  5. Altering files in another file system??
    By Raavin in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2001, 09:18 AM