Click here to Skip to main content
15,564,100 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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..

C++
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();
Posted
Updated 15-Feb-11 18:09pm
v2
Comments
TweakBird 15-Feb-11 23:17pm    
Edited for code blocks.

1 solution

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;
   }
}
 
Share this answer
 
v3
Comments
Yasser El Shazly 15-Feb-11 23:27pm    
when i use getLine(ifs,Line)
an error coming the error is
Could'nt fina a match for STD::getline(istream,std::basicstring
Yasser El Shazly 15-Feb-11 23:29pm    
Error: check valid night shift.cpp(31,27):Could not find a match for 'std::getline(ifstream,std::basic_string<char,std::string_char_traits<char>,std::allocator<char>>)'
Stephen Hewitt 15-Feb-11 23:31pm    
If you include the headers I did and make sure you've got the "using namespace std;" it should work.
Yasser El Shazly 16-Feb-11 1:02am    
#include <iostream.h>
#include <string>
#include <fstream>

this is the header is there is any thing missed..?
Yasser El Shazly 16-Feb-11 1:02am    
#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900