[英]TXT file is only partially read
有人會介意看看我的代碼並告訴我他們是否看到錯誤嗎? for 循環中可能存在問題。 據我所知,循環只讀取第一個學生(海綿寶寶)和他的專業,但不會超出那個學生。 關於為什么的任何想法?
#include <iostream>
#include <iomanip>
#include "gradebook.h"
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
void BuildTitle(){
cout <<endl;
cout << "Student |--------------------------------Final Final Letter" <<endl; //47 to final exam
cout << "Name |--------------------------------Exam Average Grade" <<endl; // 56 to average
cout << "----------------------------------------------------------------------" <<endl; // 65 to letter - 72 total
}
int main()
{
string input_file; //name of file to read data from
string output_file; //name of file to write data to
ifstream inFile; //file stream objects
ofstream outFile;
string str;
char fn[20]; //char array for student first name
char ln[20]; // same for last name
char subject[20]; // and for subject
int listsize; // integer for the number of students in the list to determine list size
int b_lab, b_test, b_test2, b_test3, b_final, t_part, t_mid, t_final, c1, c2, c3, c4, c5, c_test, c_final;
//declarations to store the different grades in each subject
cout << "Welcome to the Gradebook Program\n";
cout << "Please enter the name of the file which holds grades. Please only use .txt files\n";
cout << "Filename: ";
cin >> input_file;
cout << "\nOkay, now enter the name of the file you want to store grades in.\n";
cout << "Filename: ";
cin >> output_file;
cout << endl;
inFile.open(input_file.c_str()); //Open the files for reading and writing
outFile.open(output_file.c_str());
//If the file is not open
if(!inFile){
cout << "\nInput file not found or unable to be opened\n";
return 0;
};
//If the file is successfully opened
inFile >> listsize;
//below: create a dynamic list for the students
Student ** list = new Student* [listsize];
//below: truncate through the .txt file to gather information
for(int x = 0; x < listsize ; x++){
inFile.getline(ln,20,','); //third parameter changes delimiter to comma from null character
inFile.ignore(1);
inFile.getline(fn,20);
inFile.getline(subject,17);
inFile.getline(ln,20, ',');
int sub1= strcmp(subject,"Biology");
int sub2= strcmp(subject, "Theater");
int sub3= strcmp(subject, "Computer Science");
cout << ln <<"," <<fn << endl;
cout << subject << endl;
//above cout statements are only to check to see if the loop is working correctly
}
(這只是一部分代碼。它讀取的.txt文件是:
6
Squarepants, Spongebob
Computer Science 90 72 85 96 100 88 80 91 92
Finklebottom, Joe
Biology 85 90 78 85 89
Dipwart, Marvin
Theater 85 72 95
van Houten, Milhouse
Computer Science 45 57 26 79 54 52 60 71 63
Simpson, Homer J.
Theater 82 76 74
Cyrus, Miley
Biology 74 65 58 62 71
正如我所見
inFile.getline(subject,17);
inFile.getline(ln,20, ','); // <----- reads numbers after subject name
旨在讀取數字部分
Computer Science 90 72 85 96 100 88 80 91 92
但是這一行沒有 ',' 分隔符。 所以你可能會得到failbit (來自getline manual )並且不再發生讀取:
如果函數未提取任何字符,或者如果 (n-1) 個字符已寫入 s 后未找到定界字符,則設置 failbit 標志。 請注意,如果輸入序列中那些 (n-1) 個字符后面的字符恰好是定界字符,那么它也會被提取並且不設置失敗位標志(提取的序列正好是 n 個字符長)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.