| | |
I/O question. using vc++ read char by char
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2006
Posts: 4
Reputation:
Solved Threads: 0
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:
if someone has any idea...:rolleyes:
thanks in advance
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)
#include "stdafx.h" #include <iostream> using namespace std; #using <mscorlib.dll> using namespace System; using namespace System::IO; int _tmain(int argc, char* argv[]) { // Check for required arguments if (argc < 2) { Console::WriteLine(S"Usage: CppReader path"); return 0; } // i have done it like that because i had problemreading the file from //the arguments but this can be solved String* path = "\Full_data_linklist.txt"; //new String(argv[1]); if (!File::Exists(path)) { Console::WriteLine(S"Invalid filename!"); return -1; } try { //open file for reading FileStream* fs = new FileStream(path, FileMode::Open); StreamReader* sr = new StreamReader(fs); //create new file for copying FileStream* fs2 = new FileStream(S"dokimi2.txt", FileMode::Create); StreamWriter* sw = new StreamWriter(fs2); int count = 0; for(;;) { String* line = sr->ReadLine(); count++; // If there are no more lines, break out of the loop if (line == 0) break; Console::WriteLine(line); // copy the text in another file //copying line be line sw->WriteLine(line); } // Close up the file sw->Flush(); sw->Close(); Console::WriteLine(S"-- end --"); } catch(System::Exception* pe) { Console::WriteLine(pe->ToString()); } return 0; }
if someone has any idea...:rolleyes:
thanks in advance
•
•
Join Date: Jul 2005
Posts: 1,677
Reputation:
Solved Threads: 262
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).
![]() |
Similar Threads
- reading a char from keyboard without pressing "Enter"??? (C)
- How to extract a substring from a char* (C++)
- Read and write system calls <C programming> (C)
- Read a single char using Microsoft VC 6 or NET (C++)
- need help with char statement (C++)
- Is this how to read in char only (C++)
Other Threads in the C++ Forum
- Previous Thread: Real-time simulation problem
- Next Thread: Make a non-blocking windows Socket?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output 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 visual visualstudio win32 windows winsock wordfrequency wxwidgets






