954,178 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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:

#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 ;)

costantinos
Newbie Poster
4 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

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).

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

costantinos
Newbie Poster
4 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You