使用C ++从文本文件读取数据到文件的开头到文件的结尾 [英] Reading Data From Text File From the begging of the file to the end of the file Using C++

查看:48
本文介绍了使用C ++从文本文件读取数据到文件的开头到文件的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个具有480数据线的文本文件,当我开始读取数据时,我使用索引来计数已读取的行数,并且我发现索引开始从180到480计数(如您在代码中看到的那样),可以为您提供帮助我使它从第1行返回数据到行,并且我逐行从中获取数据,您应该知道它们之间有空行,我不需要它们..

Hi I have an text file having 480 Data Line and when i begins to read the data i use an index to count the number of lines were read and i found that the index begin count from 180 to 480 as you see in the code can you help me in making it return the data from line 1 to line and i take the data from it line by line and you should know there is empty line in between and i did not need them thanks..

ifstream rfile;
 rfile.open("1.txt");
 char Out[256];
 char check;
 int index=0;
 rfile.seekg(0,ios::cur);
   while(!rfile.eof())
   {
      index++;
      rfile>>Out;
      
      index;
      for(int i =0;i<7;i++)
      cout<<Out[i];
      cout<<"   --->   ";
      if(Out[0]=='N' && Out[1]=='N' && Out[2]!='N' && Out[3]!='N' &&  Out[4]!='N' && Out[5]!='N' && Out[6]!='N')
        cout<<"  Valid"<<endl;
      else if (Out[0]!='N' && Out[1]!='N' && Out[2]=='N' && Out[3]=='N' && Out[4]!='N' && Out[5]!='N' && Out[6]!='N')
        cout<<"  Valid"<<endl;
      else if (Out[0]!='N' && Out[1]!='N' && Out[2]!='N' && Out[3]!='N' && Out[4]=='N' && Out[5]=='N' && Out[6]=='N')
        cout<<"  Valid"<<endl;
      else
       cout<<"UNValid"<<endl;

   }
 rfile.close();

推荐答案

尝试类似的方法来读取文件(不要像您一样使用char这样做,您就不必担心缓冲区溢出等).
Try something like this to read the file (don''t use chars like you''re doing and you won''t have to worry about buffer overflows and such).
#include <string>
#include <iostream>
#include <fstream>
 
void main()
{
   using namespace std;
 
   ifstream ifs("1.txt");
   if (!ifs)
   {
       cerr << "Error opening file!" << endl;
       return;
   }
 
   string line;
   while (getline(ifs, line))
   {
       cout << line << endl;
   }
}


这篇关于使用C ++从文本文件读取数据到文件的开头到文件的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆