我是C ++语言编程的新手。我试图用C ++语言读取文本文件(.txt文件)。
我的文字文件的名称为 test.txt ,竞赛如下:
1,3,5,7,9,8,1,2,3
0,2,4,6,8,8,1,2,3
1,3,5,7,9,8,1,2,3
0,2,4,6,8,8,1,2,3
我按照下面的代码阅读test.txt:
#include<stdio.h>
#include<iostream>
#include<string>
#include<sstream>
#include<stdlib.h> // atoi -> Convert string to int number
#include<fstream> // need for read file
using namespace std;
int main(){
string tenfile;
cout<<"Ten file: ";
cin >> tenfile;
cout<<tenfile.c_str()<<endl;
int i,j;
int value;
string data; // declare data in file
ifstream myfile (tenfile.c_str());
while(myfile.good())
{
getline(myfile,data,','); // use ' ' not " "
cout << "data: " << data << endl;
}
myfile.close();
return 0;
}
当我运行此代码来读取test.txt时,我在屏幕上看到如下:
Ten file: test.txt
test.txt
data: 1
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
0
data: 2
data: 4
data: 6
data: 8
data: 8
data: 1
data: 2
data: 3
1
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
...
当我看到屏蔽值时,我发现第2行,第3行和第4行的第一个值没有数据:。我只看到了0,1,0。
我该如何解决这个问题?我想在我的C ++代码中解决这个问题,并且不想修改test.txt文件。
谢谢,
答案 0 :(得分:2)
输入以逗号','分隔,而不是getline(myfile,data,',');
中的新行字符。所以当你到达文本文件中一行的末尾时,getline函数看到的下一个字符序列,直到下一个逗号为“3 \ n0”,其中'\ n'是一个换行符。
然后,程序打印“3 \ n0”,输出为:
data: 3
0
您的程序仍在打印输出。
一个简单的解决方案是确保文本文件中每行的末尾都替换为逗号,以便所有值都在一行上。
另一个解决方案是检查您的数据是否包含换行符,并根据换行符的位置拆分字符串。
您也可以将程序更改为整行读取,然后用逗号字符拆分或标记数据。
更多关于字符串标记化的信息:
Multiple methods of string tokenizing
Tokenizing a string with strstream
答案 1 :(得分:0)
我为isdigit
函数添加了一个库,并改变了您读取文件的方式。您可以在<<------
#include<stdio.h>
#include<iostream>
#include<string>
#include<sstream>
#include<stdlib.h> // atoi -> Convert string to int number
#include<fstream> // need for read file
#include<cctype> // <<------ Included this for isdigit
using namespace std;
int main() {
string tenfile;
cout << "Ten file: ";
cin >> tenfile;
cout << tenfile << endl; // <<------ Removed .c_str() you don't need it
int i, j;
int value;
ifstream myfile(tenfile); // <<------ Removed .c_str() you don't need it
char c;
while (myfile.get(c)) // <<------ Reading file char by char
{
//getline(myfile, data, ','); // use ' ' not " "
if (isdigit(c)) // <<------ If char is a digit
cout << "data: " << c << endl; // <<------ Print that char
}
myfile.close();
return 0;
}
这是上述代码的结果:
data: 1
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
data: 0
data: 2
data: 4
data: 6
data: 8
data: 8
data: 1
data: 2
data: 3
data: 1
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
data: 0
data: 2
data: 4
data: 6
data: 8
data: 8
data: 1
data: 2
data: 3
如果您不想仅为isdigit
函数添加新库,可以自己编写。基本上这个:
int check_digit (char c) {
if ((c>='0') && (c<='9'))
return 1;
return 0;
}
之后,您可以check_digit(c)
代替isdigit(c)
,然后移除#include<cctype>
。
可以读取任意数量的版本:
#include<iostream>
#include<string>
#include<fstream> // need for read file
#include <locale>
using namespace std;
int count_words(const char*);
int main() {
string tenfile;
cout << "Ten file: ";
cin >> tenfile;
cout << tenfile << endl; // <<------ Removed .c_str() you don't need it
ifstream myfile(tenfile); // <<------ Removed .c_str() you don't need it
unsigned int i;
string full_text;
getline(myfile, full_text, static_cast<char>(myfile.eof()));
for (i = 0; i < full_text.size(); i++)
{
if (full_text[i] == '\n')
full_text[i] = ' ';
if (full_text[i] == ',')
full_text[i] = ' ';
}
const unsigned int word_count = count_words(full_text.c_str());
unsigned int k = 0;
string display_text;
for (i = 0; i < word_count; i++)
{
while (full_text[k] != ' ')
{
display_text += full_text[k];
k++;
if(k > full_text.size())
{
k = full_text.size();
break;
}
}
if (full_text[k] == ' ')
k++;
cout << "data: " << display_text << "\n";
display_text.clear();
}
myfile.close();
return 0;
}
int count_words(const char* str)
{
bool in_spaces = true;
int num_words = 0;
while (*str != NULL)
{
if (*str == ' ')
{
in_spaces = true;
}
else if (in_spaces)
{
num_words++;
in_spaces = false;
}
++str;
}
return num_words;
}
输入文件:
10,3,5,7,9,8,1,2,3
0,2,4,6,198,8,1,2,3
1,3,5,7,9,8,1,2,3
0,2,4,6,8,8,1,2,3000
输出:
Ten file: binStr.txt
binStr.txt
data: 10
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
data: 0
data: 2
data: 4
data: 6
data: 198
data: 8
data: 1
data: 2
data: 3
data: 1
data: 3
data: 5
data: 7
data: 9
data: 8
data: 1
data: 2
data: 3
data: 0
data: 2
data: 4
data: 6
data: 8
data: 8
data: 1
data: 2
data: 3000
答案 2 :(得分:0)
我做了这些改动:
std
符号#include
所以我的代码版本如下:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::ifstream;
using std::istringstream;
using std::string;
int main()
{
auto tenfile = string{};
cout << "Ten file: ";
getline(cin, tenfile);
cout << tenfile << endl;
auto myfile = ifstream{tenfile};
auto line = string{};
while (getline(myfile, line))
{
auto myline = istringstream(line);
auto data = string{};
while (getline(myline, data, ','))
{
cout << "data: " << data << endl;
}
}
myfile.close();
}