[英]Trying to Store Text from text file in an Array C++
int main(int argc, char* argv[]) {
ifstream ifs(argv[1],ios::in);
if(argc!=2) {
cout << "Please type main.exe and file name to run the program! Please Try Again" << endl;
}
ifs.open(argv[1]);
if (!ifs.is_open()) {
cout << "wrong file name! please open again!" << endl;
}
char line[80];
char *point;
while(ifs.getline(line,80)) //problem seem to be here
{
cout << "line =" << line << endl;
point = strtok(line, " ");
while(point!=NULL) {
if (checkdigit(point))
numberofdight++;
else if(checkkeyword(point))
keywords++;
else { }
}
cout << point <<endl;
point = strtok(NULL, " ");
}
}
ifs.close();
}
程序没有通过while 循环 while(ifs.getline(line,80)) 。 有人能帮助我吗? 编程的新手。 假设将文本文件中的所有内容存储在数组中。 TIA
所以你说这个问题的地方似乎在这里,我会围绕这个问题给出一个答案。
首先,我建议您阅读getline() API以了解它的工作原理,这将有助于更长时间的运行。 (因为你提到你是新人)
就您对 API 的使用而言.. while(ifs.getline(line,80))
ifs.eof()
检查文件是否为空。 此API 的参考。因此,这两种可能性可能是您的控件无法进入的原因,同时进行 调试并找出实际原因。 如果还没有解决,把你正在阅读的文件。
编辑:用你的文件试试这个代码。 工作代码供您参考。 (灵感来自您的问题陈述代码)
注意:在使用它们之前阅读所有 API
#include<iostream>
#include<fstream>
int main(int argc, char* argv[]) {
ifstream ifs;
if(argc!=2) {
std::cout << "Please type main.exe and file name to run the program! Please Try Again" << std::endl;
}
ifs.open(argv[1], std::ifstream::in);
if (!ifs.is_open()) {
cout << "wrong file name! please open again!" << endl;
}
char line[80];
char *point; //NOT needed for my samplecode, have just kept from your code
while(ifs.getline(line,80)) //problem seem to be here
{
cout << "line =" << line << endl;
//TODO:: Write your business logic here.
}
ifs.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.