Start from a certain line in a text file..

I have a .txt which contains some character and a lot of numbers..
I've made and array, which can contain all these numbers, but don't know how to start from that line where the numbers begin, i only want's to store the numbers since my array is a int type.. How do i read from a certain line to the end of a .txt file?

Last edited on
You'll need to make the stream skip ahead a bit. This function may come in handy:
http://www.cplusplus.com/reference/istream/istream/ignore/

You'll need to call it once for each line you want to skip if you know what you're doing.

-Albatross
Does the text file vary in format?
Or is it the same textfile format, e.g.
word1
word2
numbers
numbers
numbers

If it is always the same format, you could

infile.ignore(1000, '\n');
That skips one line in the .txt file.

If you have something like:
word1
word2 numbers
numbers

Then you would have to ignore the first line, and ignore word2 up to a certain punctuation, or use:
1
2
string temp;
infile >> temp;
Then get the numbers.
It seesm like pured Ignore code does the trick..

Now is my problem, how I insert/store the numbers in my the array..

I would think that i should use somekind of temp. storage, but how?

Here is the code piece which intialise the array, and should store every value..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 int **billy;	//Dyn. array
  billy = new int*[length];
  for(int i = 0 ;i<height;i++)
  {billy[i] = new int[height];}
 int value;
 while(is.good()){
	//is.ignore(1000, '\n');
	//is.ignore(1000, '\n');
	//is.ignore(1000, '\n');
	is.seekg(0,ios::cur) >> value;
	
	for(int i = 0 ; i<height ;i++){
	 for(int j = 0;j<length;j++) 
	 {
		 billy[i][j] = value;
	 }				
}								 

The problem is that the only value stored is the last one..
Last edited on
You don't need a while loop as well as nested for loops.
1
2
3
4
5
6
    for (int i = 0; i<height; i++)
        for (int j = 0; j<length; j++) 
            is >> billy[i][j];
    
    if (!is)
        cout << "Failed to read into array" << endl;
Why not...
Aren't you using a nested for loop???
The while checks if I reached the end of the txt file.

I tried yours, and it doesn't work.. It stores the same garbage value..
My point was, your code had three nested loops, I reduced it to two. I also made a significant change by moving the is >> operation from its position outside both for loops, and placed it inside the innermost loop.

I didn't test the code so far, there may be a bug which I didn't notice.

One of the difficulties i had was in understanding the ignore() statements, as I'm not clear on the contents of the file. A sample of the data might be useful.

I have a .txt file containing Numbers and characters.

the textfile format is like this

Text
text
number
Number
number
...

The text portion is used to determine the size..
Ignore is used to skip the fist lines, and then begin reading the file from where the numbers are...
Last edited on
Thanks for the reply. Just one more question, and I don't know whether this is even a sensible one or not. I noted from the earlier code that you have a variable size array, determined by the values of length and height. Is your code currently reading those two values from the file, or are they derived in some other way, for example user input.

Sorry about muddying the waters here, since I know this isn't what you were asking about. However it seemed relevant if I wanted to test your code myself.
the .txt file contains the length and height off the array..
so no userinput..
The text portion will always contain information about the length and the height.
Ok, thanks. Here's my example. It may not fit your data structure, but some of it should be useful.

Data file input:
8
5
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173

Program code:
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
#include <iostream>
#include <fstream>
#include <iomanip>

    using namespace std;

int main()
{
    const char * filename = "input.txt";
    ifstream is(filename);
    if (!is)
    {
        cout << "Unable to open file: " << filename << endl;
        return 1;
    }

    int height = 0, width = 0;

    if (!(is >> height >> width)  )
        cout << "Error reading dimensions" << endl;
    else
        cout << "Height: " << height << "  Width: " << width << endl;

    // Dynamic array
    int **billy;
    billy = new int*[height];
    for (int i = 0; i<height; i++)
       billy[i] = new int[width];

    // Read the data
    for (int i = 0; i<height; i++)
        for (int j=0; j< width; j++)
            is >> billy[i][j];
    if (!is)
        cout << "Error reading into array" << endl;

    // Display the data
    for (int i = 0; i<height; i++)
    {
        for (int j=0; j< width; j++)
            cout << setw(5) << billy[i][j];
        cout << endl;
    }
    cout << "done" << endl;

    // Delete the array
    for (int i = 0; i<height; i++)
        delete [] billy[i];
    delete    [] billy;

    return 0;
}

Output:
Height: 8  Width: 5
    2    3    5    7   11
   13   17   19   23   29
   31   37   41   43   47
   53   59   61   67   71
   73   79   83   89   97
  101  103  107  109  113
  127  131  137  139  149
  151  157  163  167  173
done

I don't understand the read data section..
Using my while i cann Cout the content, but not store it.
"is" is reffering to the class, but how can it reffer to a line?
By the way, in an earlier post there is this code, which seems incorrect:
1
2
3
4
 int **billy;	//Dyn. array
  billy = new int*[length];
  for(int i = 0 ;i<height;i++)
  {billy[i] = new int[height];}


I think the values of height and length don't seem to be used consistently. I would have expected something more like this:
1
2
3
4
    int **billy;
    billy = new int*[height];
    for (int i = 0 ; i<height; i++)
        billy[i] = new int[length];
Ahh.. yeah.. now it works. :)
Topic archived. No new replies allowed.