read a file line by line

Hello experts,

I'm very stuck with this because I'm a super beginner/...
I've got a txt file that contains multiple lines as below:

AB, C, 1111
C, D, 10156
XY, CC, 123144

What I'd like to do is reading the file line by line, and subtract 1 from each number
Then, if the input is as above, the result will be:

AB, C, 1110
C, D, 10155
XY, CC, 123143


I'm pretty sure there is a much easier way to do this but this is what I've written.. and this does not work :(
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
struct line
{
	string alph;
	int num;

};

int main(void)
{
	std::ifstream infile("file.txt");

	line m_line;

	std::string line;
	while (std::getline(infile, line))
	{
		string tempPos;
		for (int i = 0; i < line.length(); i++)
		{
			if (isalpha(line[i]) || line[i] == ',' || line[i] == ' ')
				m_line.alph += line[i];
			if (isdigit(line[i]))
				tempNum += line[i];
		}

		int value;
		istringstream(tempNum) >> value;
		m_line.num = value;

		cout << m_line.alpha << m_line.num << endl;


	}

}


I would appreciate any other algorithms to do this or any ideas to fix this code
Thank you so much
Last edited on
In your readLine function, you return a pointer to the line array (Strictly speaking, a pointer to its first character, but the difference is irrelevant here). Since it's an automatic variable (i.e., it's “on the stack”), the memory is reclaimed when the function returns. You see gibberish because printf has put its own stuff on the stack.

You need to return a dynamically allocated buffer from the function. You already have one, it's lineBuffer; all you have to do is truncate it to the desired length.

lineBuffer[count] = '\0';
realloc(lineBuffer, count + 1);
return lineBuffer;
}

Getting the Know about the <a href="http://www.cetpainfotech.com/technology/c-language-training">C Language Training</a>
Here are two possibilities, but there are many more.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

string filter1( string line );         // Use either
string filter2( string line );         // ...


int main()
{
   string line;
   ifstream infile( "file.txt" );
   while ( getline( infile, line ) ) cout << filter2( line ) << '\n';
}


string filter1( string line )
{
   string part[3];                                             // assumes three entities per line
   stringstream ss( line );                                    // set up a stringstream
   for ( int i = 0; i < 3; i++ ) getline( ss, part[i], ',' );  // split at commas
   part[2] = to_string(   stoi( part[2] )   - 1 );             // use stoi to get integer; subtract 1; convert back to string
   return part[0] + "," + part[1] + ", " + part[2];            // reassemble string
}


string filter2( string line )
{
   int pos = line.find_first_of( "0123456789" );               // locate the number
   string part = line.substr( pos );                           // grab the number part of the string
   part = to_string(   stoi( part )   - 1 );                   // use stoi to get integer; subtract 1; convert back to string
   return line.substr( 0, pos ) + part;                        // reassemble string
}


AB, C, 1110
C, D, 10155
XY, CC, 123143
Last edited on
Topic archived. No new replies allowed.