Thread: Filestream Not Seeking to the Beginning

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Filestream Not Seeking to the Beginning

    Can someone please explain to me what I'm doing wrong. The output is clearly wrong. The first value in the array is 73, therefore I don't think it's seeking to the beginning of the file properly, which would explain why the rest of the values are -1.

    The file just contains 10 words on separate lines of various lengths.

    Code:
    int Process_File(arguments file_args) {
     
    	ifstream fd(file_args.file, ios::in); // file_args.file = file path
     
    	if (!fd) {
    		printf("File open error.\n");
    		return 0;
    	}
     
    	char tmp[50];
    	int lines;
     
    	while (fd) {	
    		fd.getline(tmp, 50);
     
    		if (fd)
    			lines++;
    	}
     
    	printf("There are: %d lines.\n", lines);
     
    	int * offsets = new int[lines];
     
    	fd.seekg(0, ios_base::beg);
     
    // Supposed to store the character positions of each new line in an array
    // Array index represents the line number, value represents character position
     
    	for (int i = 0; i <= lines; i++) {
    		offsets[i] = fd.tellg();
    		fd.getline(tmp, 50);
    	}
     
    	for (int i = 0; i <= lines; i++)
    		printf("line: %d, character: %d\n", i, offsets[i]);
     
    	return 0;
    }
    Code:
    Output:
     
    There are: 10 lines.
    line: 0, character: 73
    line: 1, character: -1
    line: 2, character: -1
    line: 3, character: -1
    line: 4, character: -1
    line: 5, character: -1
    line: 6, character: -1
    line: 7, character: -1
    line: 8, character: -1
    line: 9, character: -1
    line: 10, character: -1
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Update: Ok so I thought I'd give it a shot with a vector instead... But now the problem is with printing the 5th line (as a test to see if it's working properly). The math seems fine, I can count in the file and see that the numbers are correct, but it isn't printing the 5th line, it's just printing nothing...

    This leads me to believe it's a problem with seekg(), and instead of it seeking to word[4] it's not working, so it's reading nothing (from past the end of the vector).

    So I must be doing something wrong with tellg and seekg... I can't get either of them to do what they're supposed to do (unless it turns out that those aren't the real problem and I'm missing the real problem).

    Code:
    int Process_File(arguments file_args) {
     
    		ifstream fd(file_args.file, ios::in);
     
    	if (!fd) {
    		printf("File open error.\n");
    		return 0;
    	}
     
    	char tmp[50];
    	int lines = 0;
     
    	vector<int> words;
     
    	while (fd) {		
    		words.push_back(fd.tellg());
    		fd.getline(tmp, 50);
     
    		if (fd)
    			lines++;
    	}
     
    	printf("There are: %d lines.\n", lines);
     
    	for (unsigned int i = 0; i < words.size(); i++)
    		printf("line: %d, character: %d\n", i, words[i]);
     
    	fd.seekg(words[4]);
    	fd.read(tmp, words[5] - words[4]);
     
    	printf("(pos: %d len: %d) Line 5 contains: %s\n", words[4], words[5] - words[4], tmp);
     
            return 0;
    }
    Here's the output. I chopped off a lot of the results because it's quite large:

    Code:
    There are: 73 lines.
    line: 0, character: 0
    line: 1, character: 7
    line: 2, character: 14
    line: 3, character: 20
    line: 4, character: 30
    line: 5, character: 36
    line: 6, character: 51
    line: 7, character: 57
    line: 8, character: 62
    line: 9, character: 73
    (pos: 30 len: 6) Line 5 contains:
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You first use the stream to read the file to the end, which sets the eof bit. You'll need to clear() that error condition before you can use the stream again.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting type
    By AngKar in forum C# Programming
    Replies: 3
    Last Post: 05-26-2006, 09:06 AM
  2. Replies: 15
    Last Post: 05-13-2006, 09:28 PM
  3. writing to the beginning of a file
    By cloudy in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2006, 01:47 PM
  4. writing to the beginning of the file
    By Jules in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 01:31 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM