Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

more string & structure angst!

leilanileilani Member Posts: 7
[b][red]This message was edited by leilani at 2003-2-14 0:28:32[/red][/b][hr]
[b][red]This message was edited by leilani at 2003-2-14 0:18:37[/red][/b][hr]
Hello,
I am extremely confused about handling strings in C. I need to read records from a file into an array of structures. A sample line from the file is:

Doe, Jane 888-88-8888 6 J 4300.00
(the name is of a fixed width of 30)

I am not able read the lines of data into the array of structures.


typedef struct
{
char name[31];
char ssn[11];
char credits[2];
char grade[2];
float tuition;
} Student;

to read a single record:

Student Stu;
char lname[31], fname[12];

fscanf(fp,"%s%s%s%s%s%f",
lname,
fname,
Stu.ssn,
Stu.credits,
Stu.grade,
Stu.tuition);
strcat(lname,fname);
strcpy( Stu.Name, lname);

I also need to do some calculations with the tuition values. Is it better to read the income as a string then convert it with the atof function?






Comments

  • scuba22scuba22 Member Posts: 61
    i am not familiar with C but I can laod data into a struct in C++:


    [code]struct Student{
    char lastname[20];
    char firstname[20];
    double exams[4];
    double average;
    char letgrade;
    bool passed;};
    //prototypes
    void Load(Student[], int&);
    void PrintFull(Student[]);

    const int MAXSTUDENTS = 25; //max size of data


    ifstream infile("C:\IFF.txt");
    ofstream outfile("C:\OFF.txt");//state your files in & out

    int main()
    {
    if(!infile)
    {
    cerr << "Cannot open input file" << endl;

    } //if

    if(!outfile)
    {
    cerr << "Cannot open output file" << endl; //trap extern files
    } //if

    int ns = 0; // number of student structs

    Student ClassList[MAXSTUDENTS];//create an array of type student

    Load(ClassList, ns); //call your load function
    PrintFull(ClassList); //print loaded array of student

    return 0;
    }


    /Load=============================
    void Load(Student CL[], int& ns)
    {

    //as long as there is data...read the files and put sequential data into place...
    int y =0;
    while (infile >> CL[ns].lastname)

    {
    infile >>(CL[ns].firstname);

    for (y = 0; y < 4; y ++)
    {
    infile >> CL[ns].exams[y];


    }//for
    ns++;

    }//while



    }



    // printFull===================================================
    void PrintFull(Student CL[]){

    outfile << "
    ";
    outfile<< setw(35) << "STUDENT (LAST, FIRST)" << setw(28) << "AVERAGE / LETTERGRADE" << setw(20) << "PASS ?" << endl;

    for (int x = 0; x < 13; x++){

    outfile << setw(20) << CL[x].lastname << ",";
    outfile << CL[x].firstname<<setw(28)<< CL[x].average;
    outfile <<" - "<< CL[x].letgrade;

    if (CL[x].passed == true)
    outfile<<setw(25) << "True" <<endl;
    else
    outfile<<setw(25) << "False" << endl;
    }

    }
    [/code]
    hope this helps a little.....
    you would read...
    while (data)
    infile >> Student studentList[ns].LastName etc.....
    i wish i could do c syntax for you...but i am learning as well!-------------------------------------------------------------


    [b][red]This message was edited by leilani at 2003-2-14 0:28:32[/red][/b][hr]
    : [b][red]This message was edited by leilani at 2003-2-14 0:18:37[/red][/b][hr]
    : Hello,
    : I am extremely confused about handling strings in C. I need to read records from a file into an array of structures. A sample line from the file is:
    :
    : Doe, Jane 888-88-8888 6 J 4300.00
    : (the name is of a fixed width of 30)
    :
    : I am not able read the lines of data into the array of structures.
    :
    :
    : typedef struct
    : {
    : char name[31];
    : char ssn[11];
    : char credits[2];
    : char grade[2];
    : float tuition;
    : } Student;
    :
    : to read a single record:
    :
    : Student Stu;
    : char lname[31], fname[12];
    :
    : fscanf(fp,"%s%s%s%s%s%f",
    : lname,
    : fname,
    : Stu.ssn,
    : Stu.credits,
    : Stu.grade,
    : Stu.tuition);
    : strcat(lname,fname);
    : strcpy( Stu.Name, lname);
    :
    : I also need to do some calculations with the tuition values. Is it better to read the income as a string then convert it with the atof function?
    :
    :
    :
    :
    :
    :
    :

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : [b][red]This message was edited by leilani at 2003-2-14 0:28:32[/red][/b][hr]
    : [b][red]This message was edited by leilani at 2003-2-14 0:18:37[/red][/b][hr]
    : Hello,
    : I am extremely confused about handling strings in C. I need to read records from a file into an array of structures. A sample line from the file is:
    :
    : Doe, Jane 888-88-8888 6 J 4300.00
    : (the name is of a fixed width of 30)
    :
    : I am not able read the lines of data into the array of structures.
    :
    :
    : typedef struct
    : {
    : char name[31];
    : char ssn[11];
    : char credits[2];
    : char grade[2];
    : float tuition;
    : } Student;
    :
    : to read a single record:
    :
    : Student Stu;
    : char lname[31], fname[12];
    :
    : fscanf(fp,"%s%s%s%s%s%f",
    : lname,
    : fname,
    : Stu.ssn,
    : Stu.credits,
    : Stu.grade,
    : Stu.tuition);
    : strcat(lname,fname);
    : strcpy( Stu.Name, lname);
    :
    : I also need to do some calculations with the tuition values. Is it better to read the income as a string then convert it with the atof function?
    :
    [blue]If it is a text file - you need to use [b]fgets()[/b] and then parse items individually with [b]strtok()[/b].

    If the records are all the same length (but it is not a text file) - you can use [b]fread()[/b] and make it a string, because it is not a structure with a 'float', since you can see "4300.00" as a tuition. After you get a string - you again, use [b]strtok()[/b] to extract the info.[/blue]
  • leilanileilani Member Posts: 7
    Thank you, both of you, for your prompt replies!
    leilani
Sign In or Register to comment.