Questions tagged [seekg]

89 questions
3
votes
3 answers

Read from file after end-of-file is reached

I have a function named readNextString(ifstream &file , char* &pBuffer) which extracts the next string from a file, until ',' or '\n' is reached, removes whitespace at the beginning and at the end of the string, saves the rest in pBuffer and returns…
dragonator
  • 137
  • 5
  • 14
2
votes
1 answer

C++ RGB values from pixel selected by user-using seekg

I need to create a program that loads a .raw image (generic 100x100 image), asks the user to select an (x, y) coordinate within the range, and display the red, green, and blue values for said pixel using the seekg function. I'm at a loss as to how…
dennis7474
  • 21
  • 3
2
votes
2 answers

Split ifstream in n streams?

i have problems with an ifstream. I want to split the ifstream in n parts. For example n = 3: Ifstream includes first 1/3 of the file. Ifstream includes second 1/3 of the file. Ifstream includes third 1/3 of the file. std::ifstream…
mani
  • 29
  • 2
2
votes
1 answer

C++ primer 5th edition Chapter 17.5.3 fstream doesn't put new line

This is the sample code from 17 Chapter (17.5.3) of the C++ primer 5th edition book. int main(void) { fstream inOut("test.txt", fstream::ate | fstream::in | fstream::out); if (!inOut) { cerr << "Unable to open file!" << endl; …
callofdutyops
  • 318
  • 3
  • 9
2
votes
1 answer

How to append to file using seekg and seekp in c++, without using ios::app flag

I am not getting proper output while using seekp and seekg function, while on the other hand when I use ios::app for appending, program works well. How should I use seekg(), and seekp() functions for appending on a…
Mahesh Jaganiya
  • 155
  • 1
  • 2
  • 11
2
votes
0 answers

fstream and seekg not operating as intended

#include #include #include using namespace std; int main(){ fstream input("EX17.39file.txt", fstream::ate | fstream::out | fstream::in); if(!input){ cout << "Failed to open file. Exiting." << endl; …
SergeantPenguin
  • 843
  • 7
  • 16
2
votes
2 answers

Maintaining a valid position using seekg in ifstreams

I am trying to make my file parsing more robust. Using an ifstream, how can I ensure seekg keeps me in a valid position within the file? This does not work: while(m_File.good() && m_File.peek() != EOF) { ...a seekg operation moves file position…
DanDan
  • 10,462
  • 8
  • 53
  • 69
2
votes
2 answers

Seekg(ios::beg) not returning to beginning of redirected input

I am making a huffman encoder and to do so i need to read over the input (which will ALWAYS be a redirected file) to record the frequencies, then create the codebook and then read over the input again so i can encode it. My problem is that i am…
user0123
  • 259
  • 1
  • 6
  • 17
2
votes
1 answer

c++ reading from beginning of file to a specified byte

Trying to write a file that reads from the first byte in a file to a byte specified by the user. I need help on the logic. If the file just has letters a through z and I just want to read and display the first 10, for example. here's a piece of what…
tp77
  • 131
  • 2
  • 2
  • 4
2
votes
2 answers

C++ reading/writing binary mode

I'm learning C++ at school and in my opinion it's a beautiful language, but I have this annoying problem. In the text book it's written with FILE *text and scanf and printf, and I personally don't like it; I got used to cin and cout or with the <<…
Mircea Mihai
  • 167
  • 1
  • 13
2
votes
4 answers

Using seekg() when taking input from redirected stdin

So i'm trying to read in a string of characters twice using cin.get(). The input is being redirected as "program < input". So it is valid to use seekg(). As the titel says, I thought I would be able to use seekg() to save the beginning position of…
Wakka Wakka Wakka
  • 271
  • 1
  • 9
  • 16
2
votes
2 answers

seekg() function fails

I am trying to write some simple code which will read a text file but reads the first line twice. I thought this would be as simple as something like this std::ifstream file; file.open("filename", std::ios_base::in); std::string line; …
Jonnster
  • 3,094
  • 5
  • 33
  • 45
1
vote
1 answer

C++ seekg to ignore a part of a file?

I have a very simple question in C++. What is the equivalent of x = new char[length]; mystream.read(x, length*sizeof(char)); delete[] x; with seekg to ignore a part of size length of a binary file ? Thank you very much !
Vincent
  • 57,703
  • 61
  • 205
  • 388
1
vote
1 answer

Why I can not get the same string after using `seekg`?

I am tring to use seekg to move the pos and then read a line from the .txt. However, I can not get the same string(a completed line) after using seekp, though the pos is same. The compiler is MinGW 11.2.0 64-bit on x86 windows 11 Here is the…
Malloc
  • 41
  • 6
1
vote
1 answer

What kinds of input streams can you call seekg on?

I have a function (legacy) that reads the first few lines of a file to determine its type, then closes and reopens the file so it can re-read the entire file using the correct interpreter. The gist is: void readFile(const char *filename) { …
Dan
  • 5,929
  • 6
  • 42
  • 52