I/O question. using vc++ read char by char

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

Join Date: Jun 2006
Posts: 4
Reputation: costantinos is an unknown quantity at this point 
Solved Threads: 0
costantinos costantinos is offline Offline
Newbie Poster

I/O question. using vc++ read char by char

 
0
  #1
Jun 9th, 2006
hello i am stuck at an IO problem and i need some help.
I need to read a file that i have but i want to parse it char by char and store the contents in an array.
in order to explain i have a file which has two columns, ie.
###############
A1 A2
B1 B2

and i want to store this values in a two-dimensional array.
I have managed to write the code to read the file and parse the data but i can do it line be line so i cannot distinguish where the A1 stops and where the A2 starts in order to put them in the second column of the array.

the code i have written up to now is just for reading the file and copying it from one file to another.


code:



  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. #using <mscorlib.dll>
  7.  
  8. using namespace System;
  9. using namespace System::IO;
  10.  
  11. int _tmain(int argc, char* argv[])
  12. {
  13. // Check for required arguments
  14. if (argc < 2)
  15. {
  16. Console::WriteLine(S"Usage: CppReader path");
  17. return 0;
  18. }
  19.  
  20. // i have done it like that because i had problemreading the file from //the arguments but this can be solved
  21. String* path = "\Full_data_linklist.txt"; //new String(argv[1]);
  22.  
  23. if (!File::Exists(path))
  24. {
  25. Console::WriteLine(S"Invalid filename!");
  26. return -1;
  27. }
  28.  
  29. try
  30. {
  31. //open file for reading
  32. FileStream* fs = new FileStream(path, FileMode::Open);
  33. StreamReader* sr = new StreamReader(fs);
  34.  
  35. //create new file for copying
  36. FileStream* fs2 = new FileStream(S"dokimi2.txt", FileMode::Create);
  37. StreamWriter* sw = new StreamWriter(fs2);
  38.  
  39. int count = 0;
  40. for(;;)
  41. {
  42. String* line = sr->ReadLine();
  43. count++;
  44. // If there are no more lines, break out of the loop
  45. if (line == 0) break;
  46.  
  47. Console::WriteLine(line);
  48.  
  49. // copy the text in another file
  50. //copying line be line
  51. sw->WriteLine(line);
  52.  
  53.  
  54. }
  55.  
  56. // Close up the file
  57. sw->Flush();
  58. sw->Close();
  59.  
  60. Console::WriteLine(S"-- end --");
  61. }
  62. catch(System::Exception* pe)
  63. {
  64. Console::WriteLine(pe->ToString());
  65. }
  66.  
  67. return 0;
  68. }

if someone has any idea...:rolleyes:
thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,677
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: I/O question. using vc++ read char by char

 
0
  #2
Jun 10th, 2006
I'm not familiar with the I/O protocol you are using, but since you have the iostream header file included in your program you could also add the fstream header file, declare an fstream in input mode or use an ifstream (which is an fstream in dedicated only to input), and then use the >> operator to separate A1 from A2 while reading from the file. Alternatively, if you must read char by char you could check each char as it is read from stream and if it is alphanumeric add it to a string and if it isn't then terminate current string, store it where however you want and read char from file until you find the next alphanumeric char to start the next string (using the isalphanum() function would make this easier for you if you try this type of protocol).
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 4
Reputation: costantinos is an unknown quantity at this point 
Solved Threads: 0
costantinos costantinos is offline Offline
Newbie Poster

Re: I/O question. using vc++ read char by char

 
0
  #3
Jun 15th, 2006
thanks very much for the reply. indeed i have used the iostream you have told me and i have found some solution to my problem.

cheers once again
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC