| | |
Reading characters into array from file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
I've bene banging my head against a brick wall trying to work out how to do this.
I need to be able to read a text file line by line and to read each character of the line. When the character is a space, anything up to that is added to an array. Any other characters such as brackets are put into their own array element so i could have words and punctuation.
I haven't got very far as I can't find anything online so help me progress
Can anyone please get me started?
I need to be able to read a text file line by line and to read each character of the line. When the character is a space, anything up to that is added to an array. Any other characters such as brackets are put into their own array element so i could have words and punctuation.
I haven't got very far as I can't find anything online so help me progress
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> using namespace std; int main() { char ch; ifstream myFile("textfile.txt"); if (! myFile) { cout << "Error opening output fle" << endl; return -1; } while (! myFile.eof()) { myFile.get(ch); /* This is where the adding to arrays/vectors would happen */ } myFile.close(); return 0; }
Can anyone please get me started?
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
0
#2 Oct 25th, 2009
You should post a short sample text file and what needs to end up happening with that text file. The description is a start, but not quite detailed enough.
Say that's the file (or if that's not a good file, provide another one). What do you do with it? What does the array (s) hold?
My name is Bob. I live at 1234 Main St. in Sacramento, CA. My
favorite food is pizza! What's your name? Well, I have to
go to work now. I'm late; it's already 9 a.m. It was nice meeting you!Say that's the file (or if that's not a good file, provide another one). What do you do with it? What does the array (s) hold?
Last edited by VernonDozier; Oct 25th, 2009 at 1:04 pm.
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
0
#3 Oct 25th, 2009
•
•
•
•
You should post a short sample text file and what needs to end up happening with that text file. The description is a start, but not quite detailed enough.
My name is Bob. I live at 1234 Main St. in Sacramento, CA. My favorite food is pizza! What's your name? Well, I have to go to work now. I'm late; it's already 9 a.m. It was nice meeting you!
Say that's the file (or if that's not a good file, provide another one). What do you do with it? What does the array (s) hold?
so the array in your example would be
My
name
is
Bob
.
...etc
Many thanks for the reply
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
0
#4 Oct 25th, 2009
Look at ispunct and strtok. I imagine that'd be your best bet. Your delimiters are a character array of all punctuation. Read in using the >> operator to throw out white space, then split the string further into tokens using strtok.
http://www.cplusplus.com/reference/c...ctype/ispunct/
http://www.cplusplus.com/reference/c...string/strtok/
strtok may not be with the trouble since you actually want to SAVE the punctuation too. Perhaps some combination of strtok to isolate all the words, and then go through character by character and grab the punctuation.
"find" and "substr" from the "string" library could also come in handy. Since you want to keep things in order and keep the punctuations, you might not want to bother with strtok and just go through the string and find all punctuation using ispunct and keep track of the indexes. Then split the string up using substr or whatever. More than one way to do it. Look at the string, cctype, and cstring libraries, as well as getline and get.
http://www.cplusplus.com/reference/c...ctype/ispunct/
http://www.cplusplus.com/reference/c...string/strtok/
strtok may not be with the trouble since you actually want to SAVE the punctuation too. Perhaps some combination of strtok to isolate all the words, and then go through character by character and grab the punctuation.
"find" and "substr" from the "string" library could also come in handy. Since you want to keep things in order and keep the punctuations, you might not want to bother with strtok and just go through the string and find all punctuation using ispunct and keep track of the indexes. Then split the string up using substr or whatever. More than one way to do it. Look at the string, cctype, and cstring libraries, as well as getline and get.
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
0
#5 Oct 25th, 2009
I am making some progress
I'm now able to read each character from each line but the problem now is processing each character.
I still need to keep scanning each character until I reach a whitespace. say i the first character is a letter, i need to add that to a char array and keep adding letters to the char array until i reach a whtespace.
How would i do that?
Here is my code I got so far. This code can strip comments
I'm now able to read each character from each line but the problem now is processing each character.
I still need to keep scanning each character until I reach a whitespace. say i the first character is a letter, i need to add that to a char array and keep adding letters to the char array until i reach a whtespace.
How would i do that?
Here is my code I got so far. This code can strip comments
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> using namespace std; int main() { int i; int a=0; char ch; string line; ifstream myFile("scan.cm"); if (! myFile) { cout << "Error opening output fle" << endl; return -1; } while( getline( myFile, line ) ) { a++; cout << "line " << a << ":"; for (i=0; i < line.length(); i++) { if(line[0] =='/' && line[1] == '*') continue; if(line[0] =='*') continue; if(line[0] =='*' && line[1] == '/') continue; cout << line[i]; //this is where the array needs to be or a vector } cout << endl; } myFile.close(); return 0; }
Last edited by AdRock; Oct 25th, 2009 at 7:55 pm.
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
1
#6 Oct 25th, 2009
Is this a code parser? I see you are looking for "/*" and "*/", which are comment delimiters. if not, what is the significance of the tests on lines 29 through 33? Since whitespace is a delimiter and not just newline, I'd go with the >> operator rather than getline. >> separates all whitespace for you.
Read in using >> and it splits to this:
Next task with each word is to split into punctuation. I'd go through character by character looking for punctuation and grabbing their indexes.
Character at index 3 is punctuation, indexes are 0 through 4, so three substrings:
Stick the three strings in the array, go on to the next word. Decide exactly what "punctuation" is and go through a character at a time. If punctuation is what is defined in "ispunct", use it. Otherwise write your own. If the "tokens" are more complicated than single characters, this approach won't work.
I'm going to the Brown's house for dinner.Read in using >> and it splits to this:
I'm
going
to
the
Bob's
house
for
dinner.Next task with each word is to split into punctuation. I'd go through character by character looking for punctuation and grabbing their indexes.
Bob'sCharacter at index 3 is punctuation, indexes are 0 through 4, so three substrings:
Bob // up to but not including index 3.
' // index 3
s // everything after index 3Stick the three strings in the array, go on to the next word. Decide exactly what "punctuation" is and go through a character at a time. If punctuation is what is defined in "ispunct", use it. Otherwise write your own. If the "tokens" are more complicated than single characters, this approach won't work.
Last edited by VernonDozier; Oct 25th, 2009 at 8:20 pm.
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
0
#7 Oct 25th, 2009
Many thanks
Yes I am working on a code parser and I'm getting there slowly
I've found some examples online that other people have tried for different things and i can get it working in my code sort of.
The problem i have is getting all of the data put into the vector until it reaches a whitespace. It has something to do with the do while loop
Yes I am working on a code parser and I'm getting there slowly
I've found some examples online that other people have tried for different things and i can get it working in my code sort of.
The problem i have is getting all of the data put into the vector until it reaches a whitespace. It has something to do with the do while loop
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; int main() { int i; int a=0; char ch; string line; ifstream myFile("scan.cm"); if (! myFile) { cout << "Error opening output fle" << endl; return -1; } vector < vector < char > > info; while( getline( myFile, line ) ) { vector < char > data; char value; istringstream iss(line); while (iss >> value) { if(line[0] =='/' && line[1] == '*') { continue; } else if(line[0] =='*') { continue; } else if(line[0] =='*' && line[1] == '/') { continue; } else { //do //{ data.push_back(value); //} //while (value !=' '); } } info.push_back(data); } for ( vector < vector < char > > :: size_type i = 0, size = info.size(); i < size; ++i) { cout << "line " << i + 1 << ": "; for ( vector < char > :: size_type j = 0, length = info[i].size(); j < length; ++j) { cout << info[i][j]; } cout << endl; } myFile.close(); return 0; }
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
0
#8 Oct 25th, 2009
Lines 32 through 54 - What are you trying to accomplish here? Lines 34, 38, 42 are the same test over and over again. Why test for the exact same thing every time through the loop? Just test it once. line[0] and line[1] never change in this loop. Lines 42 through 45 can be deleted. If the test on line 42 is true, then the earlier test on line 38 will have been true, so it is impossible for line 44 to execute.
Not sure what you were trying to accomplish with the commented out do-while loop, but left uncommented, it would be an infinite loop since value wouldn't ever change.
What exactly are you trying to end up with here and what's the input and what are the guidelines? What is this parser expected to be able to handle? You should post a sample file, along with what needs to end up in all of the vectors at the end based on that sample file.
Not sure what you were trying to accomplish with the commented out do-while loop, but left uncommented, it would be an infinite loop since value wouldn't ever change.
What exactly are you trying to end up with here and what's the input and what are the guidelines? What is this parser expected to be able to handle? You should post a sample file, along with what needs to end up in all of the vectors at the end based on that sample file.
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
0
#9 Oct 26th, 2009
I need to create a list of each word and punctuation and what line they appear on like this (using your example)
See with your example I would scan the first letter and keep adding it to array/vector until i reach a whitespace which would create a word. Once it reaches a whitespace, it knows the word is complete. It keeps scanning until it finds another letter and does the same but if it reaches some punctuation, it adds that to it's own array/vector.
!
See with your example I would scan the first letter and keep adding it to array/vector until i reach a whitespace which would create a word. Once it reaches a whitespace, it knows the word is complete. It keeps scanning until it finds another letter and does the same but if it reaches some punctuation, it adds that to it's own array/vector.
•
•
•
•
Line 1: My
Line 1: name
Line 1: is
Line 1: Bob
Line 1: .
Line 1: I
Line 1: live
Line 1: at
Line 1: 1234
Line 1: Main
Line 1: St
Line 1: .
Line 1: in
Line 1: Sacramento
Line 1: ,
Line 1: CA.
Line 2: My
Line 2: favorite
Line 2: food
Line 2: is
Line 2:pizza
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
0
#10 Oct 26th, 2009
I have been trying different things all day but still not getting where i need to be.
I can scan each character and output each character but it's not reading whitespace as a delimiter.
I've been looking at strtok but if you had a string like Bob's, it would take that as a whole string and not 3 separate tokens like you suggested.
How would i do what you suggested as I can't find anything that helps me and everything i try doesn't work?
Many thanks for your help btw
I can scan each character and output each character but it's not reading whitespace as a delimiter.
I've been looking at strtok but if you had a string like Bob's, it would take that as a whole string and not 3 separate tokens like you suggested.
How would i do what you suggested as I can't find anything that helps me and everything i try doesn't work?
Many thanks for your help btw
![]() |
Similar Threads
- Reading a list from txt file to array... (Pascal and Delphi)
- Reading characters from a file (C++)
- Need Help in Reading characters from a text file (C++)
- reading from custom input file?! (C#)
- Reading an array from a file (Java)
Other Threads in the C++ Forum
- Previous Thread: my code for the DynamicArray and matrix problem!!!!
- Next Thread: C++ Compare stacks help!
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






