Reading columns from a text file

How can I read just the first column of this from a text file?

1
2
3
4
5
6
7
8
9
	   ORG	       100
 	   LOAD	       X
 	   ADD	       Y
 	   STORE       Z
 	   HALT	
X,	   DEC	       49
Y,	   DEC	       -80
Z,	   HEX	       0000


I have

1
2
3
4
5
6
7
8
9
while (! sourceCode.eof() )
{
getline(sourceCode,symbolValue,'\n');

sourceCode >> symbolValue >> codeWord >> value;

symbolTable << symbolValue << endl;

}
Can someone please help me with this? I've been trying forever!
> How can I read just the first column of this from a text file?

Which is the first column?
blank, ..., blank, x, Y, Z,?
blank, ..., blank, x Y Z?
ORG LOAD ADD etc.?
I want it to read the X, Y, Z, so it'll show up as:

X,
Y,
Z,
Am I way off or am I close?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    constexpr char COMMA = ',' ;

    std::istringstream file(
                                "      ORG         100\n"
                                "      LOAD        X\n"
                                "      ADD         Y\n"
                                "      STORE       Z\n"
                                "      HALT\n"
                                "X,    DEC         49\n"
                                "Y,    DEC         -80\n"
                                "Z,    HEX         0000\n"
                            ) ;

    std::string line ;
    while( std::getline( file, line ) )
    {
        std::cout << line  << "   (column one is: '" ;

        std::string col_one ;

        if( line.find( COMMA ) != std::string::npos ) // if a comma is found
        {
            std::istringstream stm(line) ; // put the line into a string stream
            // read everything starting from the first non-whitespace character
            // upto, but not including the comma
            std::getline( stm >> std::skipws, col_one, COMMA ) ;
        }

        std::cout << col_one << "')\n" ;
    }
}

http://coliru.stacked-crooked.com/a/28fc45ad6afe2fff
I don't understand that. All I want it to do is separate that first column and put it in a file by itself. Here is my entire code so far, it won't separate anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;


main()
{

ifstream sourceCode;
ofstream symbolTable;

string symbolValue;
string codeWord;
string value;



//open files

sourceCode.open ("sourceCode.data", ios :: in);
symbolTable.open ("symbolTable.data", ios :: out);

	//symbol table

	symbolTable << "Symbol Table" << endl << endl;

	symbolTable << symbolValue << endl;

	

	while (!sourceCode.eof())

		{
		sourceCode >> symbolValue >> codeWord >> value;
		symbolTable << symbolValue << endl;

		}

}
Last edited on
> I don't understand that.

So, look up the things that you don't understand.
This is an excellent reference: http://en.cppreference.com/w/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main()
{
    using namespace std ;

    const char COMMA = ',' ;

    ifstream sourceCode( "sourceCode.data" ) ;
    ofstream symbolTable( "symbolTable.data" ) ;

    std::string line ;
    while( std::getline( sourceCode, line ) ) // read the file one line at a time
    {

        if( line.find( COMMA ) != std::string::npos ) // if the line contains a comma
        {
            std::istringstream stm(line) ; // put the line into a string stream

            // read everything starting from the first non-whitespace character
            // upto, but not including the comma into symbolValue
            std::string symbolValue ;
            std::getline( stm >> std::skipws, symbolValue, COMMA ) ;

            symbolTable << symbolValue << '\n' ; // and write it to symbolTable
        }
    }
}
I really need help with this.
I see now, thank you!
Topic archived. No new replies allowed.