Reading different # of inputs in each line of the same file

Hello all,

I'm back with yet another nooblett question.

How do read varying size lines from input. for example i have 2 int and 1 string on 1 line, on next there is 2 string 3 integers, etc.

I have the same problem for output but as far as i know we can append multiple different outputs so thats ok.

Normally i read and output stuff as follows.
1
2
3
4
5
6
7
String name, int age;
ifstream infile(InputFileName);
 while (InFile >> name >> age)
{
//do stuff
}
Outfile << st_name << ' ' << st_age << '\n';
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    ifstream fin("in.txt");
    ofstream fout("out.txt");

    string str;
    int num;

    stringstream my_strings(stringstream::out);
    stringstream my_integers(stringstream::out);

    while (true)
    {
        fin>>str;

        if (!fin) break;

        stringstream buffer(stringstream::in|stringstream::out);

        buffer<<str;
        buffer>>num;

        if (!buffer)
        {
            my_strings<<str<<'\n';
        }
        else
        {
            int size=buffer.str().size();
            bool is_string=false;
            char ch;

            for (int i=0; i<size; i++)
            {
                ch=buffer.str()[i];

                if (!isdigit(ch))
                {
                    if (i==0&&(ch=='-'||ch=='+'))
                        continue;

                    is_string=true;
                    break;
                }
            }

            if (is_string)
            {
                my_strings<<str<<'\n';
            }
            else
                my_integers<<num<<'\n';
        }

    }

    fin.close();

    fout<<"strings:\n";
    fout<<my_strings.str() << endl;
    fout<<"integers:\n";
    fout<<my_integers.str() << endl;

    fout.close();

    return 0;
}


in.txt:
234 23op sdf3rt4
sdf sdfdsf 34 233 5445 -3434
dfdf df 3434 efd 34 sadf


out.txt:
strings:
23op
sdf3rt4
sdf
sdfdsf
dfdf
df
efd
sadf

integers:
234
34
233
5445
-3434
3434
34


Last edited on
I though you knew m4ster r0shi that we don't like full solutions around these parts. Especially those without comments.

You can use getline or cin.getline to get more than just whole lines. The third parameter should you define it is a delimiting character. Set it to whatever you want, and the getline will stop there.

To separate them, then <ctype> has a nifty function known as isdigit(). If all the characters are digits (save the first character which may be a minus sign, check that on the first iteration of your loop), then it's an integer. Else, not.

I guess m4ster r0shi gave out a very long code solution, but this should clear up how to do it besides Ctrl+C and Ctrl+P.

-Albatross
First of all i want to thank m4ster r0shi for his full code but maybe i explained things wrong. I can still adjust m4ster r0shi's code but i'm sure there is an elegant way to do what i want.

Below is my input;

11 7 4 -10 -1
A E H W
4 -1 -2 -3
-1 5 0 -3
-2 0 8 -2
-3 -3 -2 11
HAAEEWWHEEA
HHAEWHA

Notice that each line holds parameters for doing something with the 2 seperate strings below. Each line has different number of variables. i.e, first line has 5 integers, 2. line has 4 characters, last 2 lines have 2 strings.

So far, i'm doing the following;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	infile >> length_of_M >> length_of_N>> s >> gap_opening_penalty>>gap_extension_penalty;
	 
 	getline(cin,alphabet);// get the letters
 	alphabet.erase( remove( alphabet.begin(), alphabet.end(), ' ' ), alphabet.end() );//remove spaces

	sstable=new int[s];// form up a SxS matrix 
	for(int i=0;i<s;i++)
		*(sstable+i)=new int[s];// error here for some reason


// read S lines in to sstable array

	getline(cin,m);// read string M
	getline(cin,n);// read string N

	infile.close();


any suggestions?
Last edited on
solved it,

thanks for everyones help
Topic archived. No new replies allowed.