Reading characters into array from file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Reading characters into array from file

 
0
  #1
Oct 25th, 2009
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
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char ch;
  9.  
  10. ifstream myFile("textfile.txt");
  11.  
  12. if (! myFile)
  13. {
  14. cout << "Error opening output fle" << endl;
  15. return -1;
  16. }
  17.  
  18. while (! myFile.eof())
  19. {
  20. myFile.get(ch);
  21. /*
  22.   This is where the adding to arrays/vectors would happen
  23.   */
  24. }
  25. myFile.close();
  26. return 0;
  27. }

Can anyone please get me started?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
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.

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training
 
0
  #3
Oct 25th, 2009
Originally Posted by VernonDozier View Post
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?
it should scan each character until it gets to a space so in the array would be a word. Any punctation should be in their own array element. I want to ignore whitespace and newlines

so the array in your example would be

My
name
is
Bob
.
...etc

Many thanks for the reply
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training
 
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

  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. int i;
  10. int a=0;
  11. char ch;
  12. string line;
  13.  
  14. ifstream myFile("scan.cm");
  15.  
  16. if (! myFile)
  17. {
  18. cout << "Error opening output fle" << endl;
  19. return -1;
  20. }
  21.  
  22. while( getline( myFile, line ) )
  23. {
  24. a++;
  25.  
  26. cout << "line " << a << ":";
  27. for (i=0; i < line.length(); i++)
  28. {
  29. if(line[0] =='/' && line[1] == '*') continue;
  30.  
  31. if(line[0] =='*') continue;
  32.  
  33. if(line[0] =='*' && line[1] == '/') continue;
  34.  
  35. cout << line[i];
  36.  
  37. //this is where the array needs to be or a vector
  38. }
  39.  
  40. cout << endl;
  41.  
  42. }
  43.  
  44. myFile.close();
  45.  
  46. return 0;
  47. }
Last edited by AdRock; Oct 25th, 2009 at 7:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
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.

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.


Character 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 3

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.
Last edited by VernonDozier; Oct 25th, 2009 at 8:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training
 
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

  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int i;
  12. int a=0;
  13. char ch;
  14. string line;
  15.  
  16. ifstream myFile("scan.cm");
  17.  
  18. if (! myFile)
  19. {
  20. cout << "Error opening output fle" << endl;
  21. return -1;
  22. }
  23.  
  24. vector < vector < char > > info;
  25.  
  26. while( getline( myFile, line ) )
  27. {
  28. vector < char > data;
  29. char value;
  30. istringstream iss(line);
  31.  
  32. while (iss >> value)
  33. {
  34. if(line[0] =='/' && line[1] == '*')
  35. {
  36. continue;
  37. }
  38. else if(line[0] =='*')
  39. {
  40. continue;
  41. }
  42. else if(line[0] =='*' && line[1] == '/')
  43. {
  44. continue;
  45. }
  46. else
  47. {
  48. //do
  49. //{
  50. data.push_back(value);
  51. //}
  52. //while (value !=' ');
  53. }
  54. }
  55. info.push_back(data);
  56. }
  57.  
  58. for ( vector < vector < char > > :: size_type i = 0, size = info.size(); i < size; ++i)
  59. {
  60. cout << "line " << i + 1 << ": ";
  61. for ( vector < char > :: size_type j = 0, length = info[i].size(); j < length; ++j)
  62. {
  63. cout << info[i][j];
  64. }
  65. cout << endl;
  66. }
  67. myFile.close();
  68.  
  69. return 0;
  70. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training
 
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.

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
!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training
 
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC