Questions tagged [seekg]

89 questions
0
votes
0 answers

changing the order of the data inside of a file using seekg, tellg function

I'm new to c++ programming. I have a small excercise which enables the use of the following: using of windows socket programming, using of window api functions (CreateFile, ReadFile, and WriteFile), and using of seekg, seekp, tellg, and tellp…
frrelmj
  • 9
  • 4
0
votes
1 answer

Seekg not behaving as expected

#include #include int main() { std::string str; char magic[9]; std::cin.read((char *)magic, sizeof(magic)); std::cin.seekg(0, std::ios::beg); while (std::cin >> str) { …
srbcheema1
  • 544
  • 4
  • 16
0
votes
0 answers

C++ Can't write file with peek ; using fstream

I create in C++ a function that write in file, by using stream. I want that file was overwrite when i call this code. When i add a fstream.peak, to know the next char, nothing was written in my text file. If they return -1, I want they write at X…
SurveyVisual
  • 27
  • 1
  • 8
0
votes
0 answers

std::ifstream::seekg not failing with absurd offset

Apparently, on Linux (Centos 7, g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)) you can seek to absurd offsets in files opened for reading. I can get the file length and check the offset myself, but shouldn't the seekg fail? This program illustrates. No…
0
votes
1 answer

Output a double written in a binary file C++

I have a class named "obj" that has two data types one int and one double. Im trying to just read in the third object but cant seem to figure it out. It was working before I changed one of the data type to double. I feel like it has to do with the…
0
votes
1 answer

Unusual behaviour of get() (reading from a file in c++)

// Print the last n lines of a file i.e implement your own tail command #include #include #include int main() { std::ifstream rd("D:\\BigFile.txt"); int cnt = 0;char c; std::string data; …
Syed Saad
  • 107
  • 2
  • 9
0
votes
3 answers

Searching for char from end of file using seekg() c++

I have a file and I want to only output the last line to the console. Here's my thoughts on how I would do it. Use file.seekg(0, ios::end) to put myself at the end of file. Then, I could create a decrement variable int decrement = -1; and use a…
Podo
  • 709
  • 9
  • 30
0
votes
2 answers

ifstream how to start read line from a particular line using c++

I'm using ifstream to parse a file in a c++ code. I'm not able using seekg() and tellg() to jump in a particular line of the code. In particular I would like to read a line, with method getLine, from a particular position of the file. Position…
rebatoma
  • 734
  • 1
  • 17
  • 32
0
votes
1 answer

How to use seekg with string input to get a record from a binary file

My sample text file look something like this: Name: First Email: first@gmail.com Name: Second Email: second@gmail.com Currently I wrote a function to read a record from specified binary file: Staff getARecord (fstream& afile, const char fileName…
Pewds
  • 65
  • 1
  • 5
0
votes
1 answer

Reading and outputting byte at position in binary file (C++)

I want to read in a set of 256 bytes from a binary (.bin) file and store it into a buffer. Then I want to move each of those bytes into a vector of char vectors. Also, I'd like to read the char values in the vector and get the corresponding…
Victor Brunell
  • 5,668
  • 10
  • 30
  • 46
0
votes
1 answer

C++: file.seekg() does not appear to return current location

I am trying to backup a line in an ifstream. file.tellg() is returning a value I was not expecting. In the example bellow, after reading the first line (a string length of 15 characters) I expected file.tellg() to return 16. Instead, it is returning…
ChrisMcJava
  • 2,145
  • 5
  • 25
  • 33
0
votes
1 answer

Understanding Seekg() and seekp() when dealing with binary files

I am a CS student, and I am trying to understand a piece of code but I can't wrap my head around it. This piece of code allows the user to modify a certain record(structure) in a binary file. I don't understand records.seekg(recNum * sizeof(person),…
user3566683
  • 5
  • 2
  • 3
0
votes
1 answer

seekg, fail on large files

I have a very large (950GB) binary file in which I store 1billion of sequences of float points. A small example of the type of file I have with sequences of length 3 could be: -3.456 -2.981 1.244 2.453 1.234 0.11 3.45 13.452 1.245 -0.234 -1.983…
David
  • 103
  • 10
0
votes
1 answer

Populating a Struct with a Binary file

I am attempting to read a binary file ("example.dat") in reverse and populating a struct of records with its contents. The file contains 10 records, and each record has three data types. #include #include using namespace…
andres
  • 301
  • 6
  • 21
0
votes
2 answers

seekg() not working as expected

I have a small program, that is meant to copy a small phrase from a file, but it appears that I am either misinformed as to how seekg() works, or there is a problem in my code preventing the function from working as expected. The text file…