| | |
Need Help in Reading characters from a text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2006
Posts: 11
Reputation:
Solved Threads: 0
Hey everyone,
I' m currently having trouble getting the following code to work.
What I'm trying to do is getting my program to do read from a text file which has the following information:
234323 c
343212 d
323432 a
763634 b
The corresponding information shown above will correspond as follow '234323' is customer ID and 'c' correspond that it belongs to '23'4323' and so on...
What I am trying to do is getting one my function to display an error message if the information obtain in the text file is not equal to a, b,c or d.
I got the first part of my function working already which required the data found in the text file to be greater then 0. If any part of the text file contain negative number it will prompt the user with an invalid input and advise them of the invalid line.
Now for second part, I need help in which required the program to read the text file. If the information contain in the text file is not equal to a, b, c or d then the program will prompt with the error line.
Here is my current code:
Any help in solving my problem would much appreciated. So feel free to reply to my post.
Regards,
tuannie
I' m currently having trouble getting the following code to work.
What I'm trying to do is getting my program to do read from a text file which has the following information:
234323 c
343212 d
323432 a
763634 b
The corresponding information shown above will correspond as follow '234323' is customer ID and 'c' correspond that it belongs to '23'4323' and so on...
What I am trying to do is getting one my function to display an error message if the information obtain in the text file is not equal to a, b,c or d.
I got the first part of my function working already which required the data found in the text file to be greater then 0. If any part of the text file contain negative number it will prompt the user with an invalid input and advise them of the invalid line.
Now for second part, I need help in which required the program to read the text file. If the information contain in the text file is not equal to a, b, c or d then the program will prompt with the error line.
Here is my current code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; const int ArrayRecord = 300; int CustID[ArrayRecord]; char StationID[ArrayRecord]; string Address[ArrayRecord]; int Checkfile(int & Record) { ifstream openfile ( "tolldata.txt" ); //Declare Input File stream as openfile //then open the call marks.txt file while (!openfile.eof() && Record < ArrayRecord && !openfile.fail()) { //While loop, As long as the statement not equal to end of file, //record is lesser then ArrayRecord, and opening file does not fail //Then perform the following. openfile >> CustID[Record]; //Open marks.txt file and store into //StudentID[Record] Array. if (CustID[Record] < 0 || openfile.fail()) /*If StudentID[Record] is greater then 999999 or lesser then 0 or record file fail to open, prompt user with error message and error line.*/ { cout << "\n\tError - Invalid or miss match Customer ID record.\n"; cout << "\tPlease refer to line " << Record +1 << " of tolldata.txt text file.\n\n" ; system( "pause" ); //perform system pause and exit //record and terminate program exit(1); return 0; } openfile >> StationID[Record]; if (StationID[Record] != 'A' || StationID[Record] != 'a' || StationID[Record] != 'B' || StationID[Record] != 'b' || StationID[Record] != 'C' || StationID[Record] != 'c' || StationID[Record] != 'D' || StationID[Record] != 'd' || openfile.fail()) { cout << "\n\tONE\n\tError - Invalid or miss match Customer ID record.\n"; cout << "\tPlease refer to line " << Record +1 << " of tolldata.txt text file.\n\n" ; system( "pause" ); //perform system pause and exit //record and terminate program exit(1); return 0; } Record++; } openfile.close(); } int main() { int Record = 0; ifstream openfile ( "tolldata.txt" ); /*Declare input file stream mark.txt*/ if (openfile.is_open())/*If text file is open excute the following*/ { cout << "\t****************************************************\n\n"; cout << "\t Please wait...\n\n" << "\t The following file tolldata.txt has been found.\n\n"; cout << "\t****************************************************\n\n"; Checkfile(Record); } else /*If text file doesn't exist display message and terminate program*/ { cout << "\t****************************************************\n\n"; cout << "\tError - Opening tolldata.txt file has fail.\n"; cout << "\tThe program will now terminate.\n\n"; cout << "\t****************************************************\n\n"; } system("pause"); return 0; }
Any help in solving my problem would much appreciated. So feel free to reply to my post.
Regards,
tuannie
I think it should be this.
if (
(
StationID[Record] != 'A'
&&
StationID[Record] != 'a'
&&
StationID[Record] != 'B'
&&
StationID[Record] != 'b'
&&
StationID[Record] != 'C'
&&
StationID[Record] != 'c'
&&
StationID[Record] != 'D'
&&
StationID[Record] != 'd'
)
||
openfile.fail()
) バルサミコ酢やっぱいらへんで
•
•
Join Date: Apr 2006
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by WolfPack
I think it should be this.
if ( ( StationID[Record] != 'A' && StationID[Record] != 'a' && StationID[Record] != 'B' && StationID[Record] != 'b' && StationID[Record] != 'C' && StationID[Record] != 'c' && StationID[Record] != 'D' && StationID[Record] != 'd' ) || openfile.fail() )
If I was to use && wouldn't that only work if all the information contain in the text file as to be a, b, c, and d for the if statement to run?
What if in the text file I was to have:
234323 c
343212 d
323432 e <=== only of these line that does not match.
763634 b
From what I remember the AND statement will only be true if the rest of the line is true. Correct me if I'm wrong.
One problem is that you are attempting to open the same file twice -- once in main() and again in Checkfile(). After opening the file in main() just pass the ifstream object to Checkfile.
Above is incorrect way to write that loop -- ifstream returns null on eof.
C++ Syntax (Toggle Plain Text)
while (!openfile.eof() && Record < ArrayRecord && !openfile.fail()) { //While loop, As long as the statement not equal to end of file, //record is lesser then ArrayRecord, and opening file does not fail //Then perform the following. openfile >> CustID[Record];
Above is incorrect way to write that loop -- ifstream returns null on eof.
C++ Syntax (Toggle Plain Text)
while (Record < ArrayRecord && openfile >> CustID[Record] ) { //While loop, As long as the statement not equal to end of file, //record is lesser then ArrayRecord, and opening file does not fail //Then perform the following.
•
•
•
•
Originally Posted by tuannie
Thank you for your respond.
If I was to use && wouldn't that only work if all the information contain in the text file as to be a, b, c, and d for the if statement to run?
What if in the text file I was to have:
234323 c
343212 d
323432 e <=== only of these line that does not match.
763634 b
From what I remember the AND statement will only be true if the rest of the line is true. Correct me if I'm wrong.
What dragon say is also correct, you are opening the file twice. You can correct them and check if my logic is correct or not.
バルサミコ酢やっぱいらへんで
•
•
Join Date: Apr 2006
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by WolfPack
I think my logic is correct. If you use ||, since 'B'!='a', you will get an error even if you input 'B'.
What dragon say is also correct, you are opening the file twice. You can correct them and check if my logic is correct or not.
To pass the ifstream from main() do i need to have my code to something like??
int checkfile(int & Record, &openfile)
{
}
•
•
•
•
Originally Posted by tuannie
how would I go about in fixing that problem by not having to open the same file again?
To pass the ifstream from main() do i need to have my code to something like??
int checkfile(int & Record, &openfile)
{
}
C++ Syntax (Toggle Plain Text)
int Checkfile(int & Record) { ifstream openfile ( "tolldata.txt" ); //Declare Input File stream as openfile if (openfile.is_open())/*If text file is open excute the following*/ { cout << "\t****************************************************\n\n"; cout << "\t Please wait...\n\n" << "\t The following file tolldata.txt has been found.\n\n"; cout << "\t****************************************************\n\n"; } else /*If text file doesn't exist display message and terminate program*/ { cout << "\t****************************************************\n\n"; cout << "\tError - Opening tolldata.txt file has fail.\n"; cout << "\tThe program will now terminate.\n\n"; cout << "\t****************************************************\n\n"; exit(1); } //then open the call marks.txt file while (!openfile.eof() && Record < ArrayRecord && !openfile.fail()) { //While loop, As long as the statement not equal to end of file, //record is lesser then ArrayRecord, and opening file does not fail //Then perform the following. openfile >> CustID[Record]; //Open marks.txt file and store into //StudentID[Record] Array. if (CustID[Record] < 0 || openfile.fail()) /*If StudentID[Record] is greater then 999999 or lesser then 0 or record file fail to open, prompt user with error message and error line.*/ { cout << "\n\tError - Invalid or miss match Customer ID record.\n"; cout << "\tPlease refer to line " << Record +1 << " of tolldata.txt text file.\n\n" ; system( "pause" ); //perform system pause and exit //record and terminate program exit(1); return 0; } openfile >> StationID[Record]; if ( ( StationID[Record] != 'A' && StationID[Record] != 'a' && StationID[Record] != 'B' && StationID[Record] != 'b' && StationID[Record] != 'C' && StationID[Record] != 'c' && StationID[Record] != 'D' && StationID[Record] != 'd' ) || openfile.fail() ) { cout << "\n\tONE\n\tError - Invalid or miss match Customer ID record.\n"; cout << "\tPlease refer to line " << Record +1 << " of tolldata.txt text file.\n\n" ; system( "pause" ); //perform system pause and exit //record and terminate program exit(1); return 0; } Record++; }
C++ Syntax (Toggle Plain Text)
int main() { int Record = 0; Checkfile(Record); system("pause"); return 0; }
バルサミコ酢やっぱいらへんで
•
•
•
•
Originally Posted by tuannie
If I was to use && wouldn't that only work if all the information contain in the text file as to be a, b, c, and d for the if statement to run?
"If input isn't 'a', AND input isn't 'A', AND input isn't 'B', AND input isn't 'b'.... etc"
You could probably cut those conditions down to a small function, if you wanted clearer code
C++ Syntax (Toggle Plain Text)
bool isValid(const char& c) { std::string acceptable("abcdABCD"); if (acceptable.find(c) == acceptable.npos) return false; //npos is the "end of string" token else return true; }
C++ Syntax (Toggle Plain Text)
if ( isValid(input_character) && !openfile.fail() )
•
•
Join Date: Apr 2006
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Bench
No, you're reading the statement wrongly, that "if" statement says
"If input isn't 'a', AND input isn't 'A', AND input isn't 'B', AND input isn't 'b'.... etc"
You could probably cut those conditions down to a small function, if you wanted clearer codeThen, later in your code you could say something likeC++ Syntax (Toggle Plain Text)
bool isValid(const char& c) { std::string acceptable("abcdABCD"); if (acceptable.find(c) == acceptable.npos) return false; //npos is the "end of string" token else return true; }C++ Syntax (Toggle Plain Text)
if ( isValid(input_character) && !openfile.fail() )
Okay, I have fixed up my code now. But one problem I'm currently having is when I executed it. It keeps saying that I have an error in line 6 when I try it try to read the following information from a text file
787867 a
145236 c
454654 a
566765 a
254788 a
This is my updated code
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; const int ArrayRecord = 300; int CustID[ArrayRecord]; char StationID[ArrayRecord]; string Address[ArrayRecord]; int Checkfile(int & Record) { ifstream openfile ( "tolldata.txt" ); //Declare Input File stream as openfile //then open the call tolldata.txt file if (openfile.is_open())/*If text file is open excute the following*/ { cout << "\t****************************************************\n\n"; cout << "\t Please wait...\n\n" << "\t The following file tolldata.txt has been found.\n\n"; cout << "\t****************************************************\n\n"; } else /*If text file doesn't exist display message and terminate program*/ { cout << "\t****************************************************\n\n"; cout << "\tError - Opening tolldata.txt file has fail.\n"; cout << "\tThe program will now terminate.\n\n"; cout << "\t****************************************************\n\n"; } while (!openfile.eof() && Record < ArrayRecord && !openfile.fail()) { //While loop, As long as the statement not equal to end of file, //record is lesser then ArrayRecord, and opening file does not fail //Then perform the following. openfile >> CustID[Record]; //Open marks.txt file and store into //StudentID[Record] Array. if (CustID[Record] < 0 || openfile.fail()) /*If StudentID[Record] is greater then 999999 or lesser then 0 or record file fail to open, prompt user with error message and error line.*/ { cout << "\n\tError - Invalid or miss match Customer ID record.\n"; cout << "\tPlease refer to line " << Record +1 << " of tolldata.txt text file.\n\n" ; system( "pause" ); //perform system pause and exit //record and terminate program exit(1); return 0; } openfile >> StationID[Record]; if (StationID[Record] != 'A' && StationID[Record] != 'a' && StationID[Record] != 'B' && StationID[Record] != 'b' && StationID[Record] != 'C' && StationID[Record] != 'c' && StationID[Record] != 'D' && StationID[Record] != 'd' && openfile.fail()) { cout << "\n\tONE\n\tError - Invalid or miss match Customer ID record.\n"; cout << "\tPlease refer to line " << Record +1 << " of tolldata.txt text file.\n\n" ; system( "pause" ); //perform system pause and exit //record and terminate program exit(1); return 0; } Record++; } openfile.close(); } int main() { int Record = 0; Checkfile(Record); system("pause"); return 0; }
Could someone please tell me why it keep saying that?
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- C++ Reading from a text file (C++)
- Help Please, how do i read from text file into array? (Visual Basic 4 / 5 / 6)
- Reading in a text file string is not complete (Pascal and Delphi)
Other Threads in the C++ Forum
- Previous Thread: Function pointers inside classes
- Next Thread: "already defined" error
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






