简体   繁体   English

尝试将文本文件中的文本存储在数组 C++ 中

[英]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();

} }

The program didn't pass through the while loop while(ifs.getline(line,80)) .程序没有通过while 循环 while(ifs.getline(line,80)) Can someone help me, please?有人能帮助我吗? Pretty new to programming.编程的新手。 Suppose to store everything from text file in array.假设将文本文件中的所有内容存储在数组中。 TIA TIA

So the place where you have told that problem seems to be here, I will give an answer around that.所以你说这个问题的地方似乎在这里,我会围绕这个问题给出一个答案。

First of I would suggest you read about getline() API to understand how it works, this will help in longer run.首先,我建议您阅读getline() API以了解它的工作原理,这将有助于更长时间的运行。 (Because you mentioned you are new) (因为你提到你是新人)

As far as your use of API goes.. while(ifs.getline(line,80))就您对 API 的使用而言.. while(ifs.getline(line,80))

  1. You can check if file is not empty using ifs.eof() .您可以使用ifs.eof()检查文件是否为空。 Reference for this API .API 的参考。
  2. If your line is longer than 80 characters then also it will skip the loop.如果您的行超过 80 个字符,那么它也会跳过循环。

So there are these two possibilities which might have been the reason for your control not going inside while, Debug and figure out actual reason.因此,这两种可能性可能是您的控件无法进入的原因,同时进行 调试并找出实际原因。 If still not solved, put the file which you are reading.如果还没有解决,把你正在阅读的文件。

EDIT : Try out this code with your file.编辑:用你的文件试试这个代码。 Working code for your reference.工作代码供您参考。 (Inspired from your problem statement code) (灵感来自您的问题陈述代码)

NOTE : Read about all APIs before using them注意:在使用它们之前阅读所有 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM