我对这个程序几乎感到沮丧。我有一个包含100条记录的文件但是,我的输出不会超过17,包括标题。谁能告诉我我做错了什么?
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
char medRecord[100][8];
int header, person;
int main()
{
ifstream in("fakedata.txt");
if(!in){
cout << "Cannot open file. \n";
return 1;
}
in.read((char *) &medRecord, sizeof medRecord);
for(person = 0; person < 100; person++)
for(header = 0; header < 8; header++)
cout << medRecord[person][header] << "";
return 0;
}
输出:
Number,Gender,GivenName,Surname,Birthday,BloodType,Pounds,FeetInches
1,male,Joseph,Doody,1/10/1968,A-,179.5,5' 7"
2,male,Robert,King,8/17/1985,A+,203.1,5' 10"
3,male,Richard,Murphy,3/18/1944,O+,235.6,6' 1"
4,female,Caroline,Acosta,5/27/1959,B+,145.4,5' 8"
5,male,John,Capps,12/18/1967,O+,186.6,5' 9"
6,female,Stephanie,Guidry,3/25/1981,O+,177.8,5' 6"
7,female,Janet,Kimmel,2/23/1977,A+,161.3,5' 7"
8,male,Jerrell,Wright,8/4/1929,B+,140.6,5' 9"
9,female,Cheryl,Johnson,12/8/1972,A+,128.7,5' 1"
10,female,Sandra,Gonzalez,6/1/1974,A+,171.4,5' 9"
11,male,Kevin,Noel,9/30/1939,O+,212.1,5' 6"
12,female,Krysta,Booth,7/9/1940,O+,173.1,5' 3"
13,male,Sam,Clark,7/5/1979,A+,162.4,5' 9"
14,male,James,Graves,8/15/1959,A+,235.0,5' 8"
15,male,Elton,Fink,6/30/1937,A+,198.4,5' 5"
16,male,Robert,Daniels,10/14/1969,Press any key to continue . . .
答案 0 :(得分:4)
您的medRecord
数组包含800个字符。输出的文本包含783个字符,一行短(或更少)为800.因此,看不见,您可能正在尝试将整个文件读入medRecord
,并且读取或输出停止时得到了充实。
我通过剪切/粘贴到Open Office来计算 - 如果新行没有被正确计算,那么783 + 17 = 800就可以了。