#How do I move the pointer to line k of a text file?
36 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Do you want to modify the text in the k line or just read the k line from the text file ?
If you wanna just ready the k line:
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream file("cos.txt");
if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
return 1;
}
int k = 5;
std::string line;
int currentLine = 0;
while (std::getline(file, line)) {
currentLine++;
if (currentLine == k) {
break;
}
}
std::cout<<line;
file.close();
}
If you wanna modify the k line than you need to rewrite the entire file
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
void writeToLine(const std::string& filename, int lineNumber, const std::string& newContent) {
std::ifstream fileIn(filename);
if (!fileIn.is_open()) {
std::cerr << "Error opening the file" << std::endl;
return;
}
std::vector<std::string> lines;
std::string line;
int currentLine = 0;
while (std::getline(fileIn, line)) {
currentLine++;
if (currentLine == lineNumber) {
lines.push_back(newContent); // Replace the content of the k line
} else {
lines.push_back(line); // Keep other lines without change
}
}
fileIn.close();
std::ofstream fileOut(filename);
if (!fileOut.is_open()) {
std::cerr << "Error opening the file" << std::endl;
return;
}
for (const std::string& l : lines) {
fileOut << l << '\n';
}
fileOut.close();
}
int main() {
std::string filename = "cos.txt";
int k= 5;
std::string newContent = "Changed text";
writeToLine(filename, k, newContent);
return 0;
}
Okay, I didn't know about the std::getline(file, line) function
I just need to move the pointer to the beginning of any specified line, that is all.
Yeah it move you a line that will get you to the start of the line (maybe I'm not understanding you if so feel free to correct me). You wanna write to this line or you wanna open a file and move the pointer to it? Moving just cursor will be done with seekg :
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main() {
std::ifstream file("example.txt");
if (!file) {
std::cerr << "Unable to open the file.\n";
return 1;
}
std::vector<std::streampos> linePositions;
std::streampos position = file.tellg();
std::string line;
while (std::getline(file, line)) {
linePositions.push_back(position);
position = file.tellg();
}
file.close();
file.open("example.txt");
int lineNumber = 5;
if (lineNumber > 0 && lineNumber <= linePositions.size()) {
file.seekg(linePositions[lineNumber - 1]); // Move to the start of the desired line
std::getline(file, line);
std::cout << "Line " << lineNumber << ": " << line << "\n";
} else {
std::cerr << "Invalid line number.\n";
}
file.close();
return 0;
}
No like: the pointer will have some location (obviously), but the only way to move it was to move it relative to either itself, the beginning of the document or the end of the document. But I am asking is whether it is possible to move the pointer to the beginning of the k-th line, without reading anything (yet, I can make that myself).
and also using getline(file, line) gives me an error
what kind of error ?
this is all the info you need
std::ifstream readEngFin("example.txt");
should i replace the condition of the while with that?
do you have this line with your file?
#include <fstream>
#include <iostream>
#include <string>
std::ifstream readEngFin("wordsEngFin.txt");
void pointerMover(int line);
int main() {
std::string wordEng;
getline(readEngFin, wordEng, '|');
std::string wordFin;
getline(readEngFin, wordFin, '|');
std::string wordFinInput;
std::cout << "What is the Finnish word for " << wordEng << "? ";
std::cin >> wordFinInput;
if (wordFinInput == wordFin) {
std::cout << "Good Job!" << std::endl;
} else {
std::cout << "No :(" << std::endl;
};
return 0;
}
void pointerMover(int k) {
int currentLine = 0;
std::string line;
while (std::getline(readEngFin, line)) {
currentLine++;
if (currentLine == k) {
break;
}
}
}
while (std::getline(readEngFin, line, '\n')) {
try like this
;compile
What is the Finnish word for ? Good Job!
the code compiles like you can see the bot. So I also run it on my own PC and it works, so the problem looks like something with the compiler on your machine.
visual studio code is at it again
ok i see
the error is saying that getline got as params (std::ifstram&, int&) ok it thinks the line arg is int (we can see it isn't)
restart of VS sometimes helps also
have a good day and let me know if error went away after the restart
Im back
Do you still have the error ?
ok nice
had some time off today would suggest to make few changes
#include <fstream>
#include <iostream>
#include <string>
void pointerMover(std::ifstream& file, int line);
int main() {
std::ifstream readEngFin("wordsEngFin.txt");
if (!readEngFin.is_open()) {
std::cerr << "Error: Could not open the file!" << std::endl;
return 1;
}
pointerMover(readEngFin, 1);
std::string wordEng;
std::getline(readEngFin, wordEng, '|');
std::string wordFin;
std::getline(readEngFin, wordFin);
std::string wordFinInput;
std::cout << "What is the Finnish word for " << wordEng << "? ";
std::cin >> wordFinInput;
if (wordFinInput == wordFin) {
std::cout << "Good Job!" << std::endl;
} else {
std::cout << "No :(" << std::endl;
}
readEngFin.close();
return 0;
}
void pointerMover(std::ifstream& file, int k) {
file.clear();
file.seekg(0, std::ios::beg);
std::string line;
for (int currentLine = 1; currentLine < k; ++currentLine) {
if (!std::getline(file, line)) {
std::cerr << "Error: Line " << k << " does not exist!" << std::endl;
return;
}
}
}
I assumed that the input is
apple|omena
banana|banaani
orange|appelsiini
!solved