我一直在努力處理這個bug一段時間。我使用windows 10和code :: blocks 16.01 MinGW。字符不一致的迴應 n
我想比較c到結束行字符。我的系統上
一個程序成功運行,只是跳過的文件的標題行:
while(c!='\n')
{
c = fgetc(traverse);
if(c == EOF)
return(1);
}
其中橫向使用
fopen("traverse.dat", "r");
然而
開了,我的其他程序:
FILE * infile;
/* Open a large CSV */
infile = fopen("Getty_Megatem.csv", "r");
if(infile == NULL)
{
printf("no file");
return(EXIT_FAILURE);
}
char c = 0;
int i = 0;
/* Opens file successfully */
printf("File opened\n");
/* Count commas in first line */
while(c != '\n');
{
c = fgetc(infile);
if(c == ',')
i++;
if(c == EOF)
return(EXIT_FAILURE);
}
printf("Number of commas: %i\n", i);
fclose(infile);
return(EXIT_SUCCESS);
and
ifstream infile;
char c;
string mystr;
infile.open("ostring.dat");
// Skip a line
while (c!= '\n')
infile >> c;
getline(infile, mystr);
和(一個我真的想工作)
ifstream emdata;
string firstline;
char c = 0;
int i = 0;
vector<double> vdata;
// redundancy
vdata.reserve(10000);
// There are ~ 300 doubles per line
emdata.open("Getty_Megatem.csv");
// Skip first line
getline(emdata, firstline);
while(c != '\n' && c != EOF)
{
emdata >> vdata[i] >> c;
cout << vdata[i] << ",";
i++;
if(i == 999)
{
cout << "\n\ni>9999";
break;
}
}
emdata.close();
return 0;
是不成功的,他們編譯和執行,然後永遠地讀取流 - 或者直到我最大迭代9999爲止。所有這些文件都包含新行。
的投入,我不明白爲什麼你不只是'函數getline()'每行? –
我想將行讀入雙向量 – Edward
您的C變體有這樣一行:'while(c!='\ n');'末尾有一個分號,它被解釋爲空語句。實際上,這說:「c'不是一個換行符,這將永遠是真的。刪除該分號。 –