944,172 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9366
  • C++ RSS
Jun 9th, 2006
0

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

Expand Post »
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:



C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
costantinos is offline Offline
4 posts
since Jun 2006
Jun 10th, 2006
0

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

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).
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jun 15th, 2006
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
costantinos is offline Offline
4 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Real-time simulation problem
Next Thread in C++ Forum Timeline: Make a non-blocking windows Socket?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC