How to read this???

so i know how to read a standart text file with chars and strings, but i came across an exercise in with a letter line that had numbers in it, so here's the text file sample:

Guolis P20
Riebokslis R33-25KT
Karbiuratorius VWG0911

so the interesting part is with second combination of letters and numbers, any help?
Last edited on
They are not numbers because they have letters in them. Read them as strings.

In fact even so called numbers such as 8739 are strings of ascii characters which the computer program interprets as a number and then converts it to how numbers are actually stored in the computer memory to be processed by the CPU or arithmetic unit. When the computer prints a number it has to convert it back to a string of numeric characters representing that number.
Last edited on
Are you trying to separate the text part from the number part? If you know the string is in [Text][Number] format, one method would be to read the string character by character until you come across the first digit, then you know where the text ends and the number begins, and convert the number substring into an int.

ex:
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 <sstream> // std::stringstream
#include <string>
#include <cctype> // isdigit()

int main() {
	std::string data = "ABC123";
	
	// Find starting index of the number part of the string,
	// assuming the "number" part of the data string is always last.
	std::size_t number_index;
	for (number_index = 0; number_index < data.length(); number_index++)
	{
		if (isdigit(data[number_index]))
		    break;
	}
	
	// Make a copy of the substring "123"
	std::string number_str = data.substr(number_index, data.length() - number_index);
        // ("ABC" can be separated by substring(0, number_index))
	
	// Convert the substring "123" into an integer 123
	std::stringstream ss(number_str);
	int n = -1;
	if (ss >> n) // put the stringstreams contents into n
	    std::cout << "Number = " << n << std::endl;
	else
		std::cout << "Unable to parse string correctly" << std::endl;
	
	return 0;
}


R33-25KT would be a bit more difficult to parse, but you didn't explain at all what form you need the end-result information to be in.
Last edited on
I just need the info "Riebokslis R33-25KT" to be in one string or in a character array, i will not do anything with them, maybe just print them out or smth.
What is the use of this C++ program?
What is the use of this C++ program?

School exercise
Check out the getline function. An example from the tutorial can be seen here in the code that reads entire lines from a file. http://www.cplusplus.com/doc/tutorial/files/#text_files
I guess i should've pointed out that this is not the entire row (sorry), it has numbers in the end:

Guolis P20 15
Riebokslis R33-25KT 20
Karbiuratorius VWG0911 30

so basically first two "words" are car parts and the number is how many of them they have. This exercise makes me work with data structures , so somehow i should read first 24 characters (it's always up to 24 until the number) and then the number.
1
2
3
4
5
6
7
8
9
char eil[24];
    fd>>n;
    for(int i=0; i<n; i++)
    {   fd.ignore('\n', 10); // i had the number on 256
        fd.get(eil, 24);
        a[i].pav= eil;
        fd>>a[i].kiek;

    }

So apperently only when the number is only on 10 , then it reads the file correctly, any thoughts on why it's so?
Topic archived. No new replies allowed.