problems reading from a file into a char arrray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <string.h>

using namespace std;

void getFirstName(char line_buff[], char first_name[],
                  const int first_name_SIZE,const int LINE_BUFF_SIZE, int &index);

void printHeaderFile(fstream &fout, const int first_name_SIZE,
                     const int last_name_SIZE, const int ID_SIZE);

void getLastName(char line_buff[], char last_name[], const int last_name_SIZE,
                 const int LINE_BUFF_SIZE, int &index);


int main()
{

    const int LAST_NAME_SIZE = 13,
              FIRST_NAME_SIZE = 11,
              LINE_BUFF_SIZE = 257,
              ID_SIZE = 7,
              TEST_SCORE_SIZE = 6;            // max number of array w + 1 for
                                      //  null terminator



    char first_name[FIRST_NAME_SIZE] = {'0'},
         last_name[LAST_NAME_SIZE] = {'0'},
         ID_num[ID_SIZE],
         line_buff[LINE_BUFF_SIZE],
         letter_grade;


    int lowest = 0,
        letter_grade_total,
        index,
        test_scores[TEST_SCORE_SIZE];

    double average;

         //Open file for read only


    ifstream fin;
    fin.open("student_input.dat", ios::in);
    if (fin.fail())
        {
        cout << endl << endl
             << "Program terminated!" << endl
             << "input file falied to open!"<< endl;

             return -1;
        }
    fstream fout;
    fout.open("student_result.dat", ios::out);
    if (fout.fail())
        {
        cout << endl << endl
             << "Program terminated!" << endl
             << "input file falied to open!"<< endl;

             return -2;
        }

    printHeaderFile( fout, FIRST_NAME_SIZE, LAST_NAME_SIZE, ID_SIZE);
// read from input file
    while (!fin.eof())
    {
       fin.getline(line_buff, LINE_BUFF_SIZE, '\n');
    //Call functions to get student info

    getFirstName(line_buff, first_name, FIRST_NAME_SIZE, LINE_BUFF_SIZE, index);
    getLastName(line_buff, last_name, LAST_NAME_SIZE, LINE_BUFF_SIZE, index);


    fout  << first_name << last_name << endl << endl;

    }

    return 0;
}
/*
char letterGrade(double average)  // function definition for letter grade cutoff.
    {
    if (average >= 89.5)
        return 'A';
    if (average >= 79.5)
        return 'B';
    if (average >= 69.5)
        return 'C';
    if (average >= 59.5)
        return 'D';
    else
        return 'F';
    }

*/
double dropLowestscore(double average, int test_scores[], const int TEST_SCORE_SIZE )
{

}


void printHeaderFile(fstream &fout, const int FIRST_NAME_SIZE,
                     const int LAST_NAME_SIZE, const int ID_SIZE)
{
    fout << setw(15)<< left << "First Name" << setw(14)
         <<"Last Name" << setw(8) <<"ID"
         << setw(17) << "Average Score"
         << setw(8) << "Grade" << endl;
}




void getFirstName(char line_buff[], char first_name[],
                  const int FIRST_NAME_SIZE,const int LINE_BUFF_SIZE, int &index)
{

    index = 0;
    memset(first_name, 0 , strlen(first_name));
    while (line_buff[index] != ' ')
    {
        first_name[index] = line_buff[index];
        index++;

    }
}



void getLastName(char line_buff[], char last_name[], const int LAST_NAME_SIZE,
                 const int LINE_BUFF_SIZE, int &index)
{

    memset(last_name, 0 , strlen(last_name));
    while(line_buff[index] != ' ')
    {
        last_name[index] = line_buff[index];
        index++;

    }


}




im trying to read from an input file into an array and i got it to work for the first name but i can get it to work for the last name.

input file looks like this

Adam Zeller 452313 78 86 91 64 90 76
Barbara Young 274253 88 77 91 66 82 93
Carl Wilson 112235 87 99 76 93 95 94



Since the first and last names are separated by a space why not just use the extraction operator to retrieve all the data?

1
2
3
4
5
6
7
8
9
10
    while (fin >> std::setw(FIRST_NAME_SIZE) >> first_name
                   >> std::setw(LAST_NAME_SIZE) >> last_name
                   >> std::setw(ID_SIZE) >> ID_num && fin.getline(line_buff, LINE_BUFF_SIZE))
    {
 

    fout  << first_name << last_name << endl << endl;

    }

Note: The code above was not tried, therefore it may not properly compile, but it should be close.


Have you studied structures, std::vector, and std::string yet? They would make processing this file much easier and much safer.



Topic archived. No new replies allowed.