How to Detect Specific Line and Write to It C++

104 Replies - 10825 Views - Last Post: 23 November 2016 - 02:47 PM Rate Topic: - - - - -

#1

  • D.I.C Head

Reputation: -4

  • View blog
  • Posts: 166
  • Joined: 18-October 16

writing to a specific line

Posted 11 November 2016 - 11:59 AM

Does anyone know how to use seekp('\n') to get to a line in an output text file? As in:

for(i < line) { seekp('\n', std::ios_base::cur) }                

Is This A Good Question/Topic? 0

  • +

Replies To: writing to a specific line

#2 Skydiver User is online

Reputation: 7906

  • View blog
  • Posts: 26,389
  • Joined: 05-May 12

Re: writing to a specific line

Posted 11 November 2016 - 12:02 PM

seekp() does not take a character as a first parameter. Did you try reading the documentation to see how to use it?


#3 jv1597 User is offline

  • D.I.C Head

Reputation: -4

  • View blog
  • Posts: 166
  • Joined: 18-October 16

Re: writing to a specific line

Posted 11 November 2016 - 12:07 PM

I tried a test compilation, went through, and tried it out. The output was written, but it skipped 7 spaces per line requested, and wrote gibberish.


#4 Skydiver User is online

Reputation: 7906

  • View blog
  • Posts: 26,389
  • Joined: 05-May 12

Re: writing to a specific line

Posted 11 November 2016 - 01:54 PM

That code you posted in post #1 won't compile. Are you sure you are using for or while. How does the loop even stop looping? What's the significance of "7 spaces"?


#5 jv1597 User is offline

  • D.I.C Head

Reputation: -4

  • View blog
  • Posts: 166
  • Joined: 18-October 16

Re: writing to a specific line

Posted 11 November 2016 - 02:01 PM

yeah, I'm using:

int lines = n; ifstream ifstr("test.txt", std::ifstream::binary); for (int i = 0; i < lines; i++) ifstr.seekg('\n', std::ios_base::cur);                

That's pretty much it, it compiles, and runs just fine. But like I said, it just writes gibberish. It should compile, I didn't have any problems with it...


#6 Skydiver User is online

Reputation: 7906

  • View blog
  • Posts: 26,389
  • Joined: 05-May 12

Re: writing to a specific line

Posted 11 November 2016 - 02:18 PM

An ifstream is an input stream. seekg() changes the read file pointer. Where is the code that writes out this so called gibberish?


#7 jv1597 User is offline

  • D.I.C Head

Reputation: -4

  • View blog
  • Posts: 166
  • Joined: 18-October 16

Re: writing to a specific line

Posted 11 November 2016 - 03:01 PM

sorry, it's:

int lines; ofstream ofstr("test.txt", std::ofstream::binary) for (int i = 0; i < lines; i++) {      ifstr.seekp('\n', std::ios_base::cur); }; ofstr.write("this is a line to write to test file");                

I got the input and output mixed up.

I would like to get this method to work, as it's really short and simple. If you have any ideas, please let me know.


#8 Skydiver User is online

Reputation: 7906

  • View blog
  • Posts: 26,389
  • Joined: 05-May 12

Re: writing to a specific line

Posted 11 November 2016 - 03:16 PM

So you are seeking the write pointer in your input stream 10 * line bytes forward, and then you are writing to your output stream. It's still unclear where the so call "gibberish" is coming from based on the code you have provided.


#9 Skydiver User is online

Reputation: 7906

  • View blog
  • Posts: 26,389
  • Joined: 05-May 12

Re: writing to a specific line

Posted 11 November 2016 - 03:25 PM

To make things easier, please post a minimal complete program that demonstrates the problem. Please also post the contents of your "test.txt" file.


#10 jv1597 User is offline

  • D.I.C Head

Reputation: -4

  • View blog
  • Posts: 166
  • Joined: 18-October 16

Re: writing to a specific line

Posted 12 November 2016 - 07:03 AM

Ok, it's really simple:

int main() { 	int linstr = 3; 	int len = 120;         char buf = new char[len]; 	string teststr = "this is a test..."; 	ofstream outfile("test.txt", std::ofstream::binary); 	 	if(outfile.is_open()) 	{ 		for (int i = 1; i <= linstr - 1; i++) 		{ 			outfile.seekp('\n', std::ios_base::cur); 		} 		outfile << teststr;                 or, outfile.write(buf, len); 	} 	else 	{ 		cout << "The file you specified could not be opened!  Please contact the source code administrator for further assistance."; 	}; 	 }                

The contents of test.txt are as follows:

This is the file
containing the data
that is to be
overwritten for
testing purposes.

It's a really simple application. I just needed to write to a specific line. I did some research and found some solutions, but I thought if this solution would work out, it would be alot simpler snippet to use than other solutions.


#11 Skydiver User is online

Reputation: 7906

  • View blog
  • Posts: 26,389
  • Joined: 05-May 12

Re: writing to a specific line

Posted 12 November 2016 - 01:03 PM

First of all, that code does not look like your previous posts where you had ifstr and ofstr. You are making it very difficult for us to help you when you keep on changing your question.

Next what were you expecting seekp('\n', ios_base::cur) call to do? Does that expectation match up with the documentation I previously linked to?


#12 snoopy11 User is offline

Reputation: 1594

  • View blog
  • Posts: 5,010
  • Joined: 20-March 10

Re: writing to a specific line

Posted 12 November 2016 - 03:45 PM

Your code as it stands apart from lines 4 + 5 which should be removed,

produces the output of 20 spaces followed by the text "this is a test..."

this is because the ascii value of '\n' is 10 and the loop executes 2 times.

2*10 = 20;

what you're after is probably something like this.

#include <iostream> #include <fstream>  using namespace std;  int main() {     int linstr = 2;      string teststr = "this is a test...";     ofstream outfile("test.txt", std::ofstream::trunc);      if(outfile.is_open())     {          for (int i = 0; i < linstr; i++)         {             outfile.put('\n');         }         outfile << teststr;      }     else     {         cout << "The file you specified could not be opened!  Please contact the source code administrator for further assistance.";     };      outfile.close();      return 0; }                

Where it writes two new lines specified by the for loop and then the text message.

Regards

Snoopy.


#13 jv1597 User is offline

  • D.I.C Head

Reputation: -4

  • View blog
  • Posts: 166
  • Joined: 18-October 16

Re: writing to a specific line

Posted 12 November 2016 - 04:28 PM

Hey skydiver,
Thanks for the post. I tried out your suggestion, it works, but it overwrites the data in the file. It's pretty good progress though. I'll do some more research to see what I can find out about it, to see if I can get it to write to the line without overwriting the data that's already in the file. By the way, I was getting 7 spaces per line request on what I was doing, the pointer was at 14, for 2 line skip, and at 21 for a 3 line skip. I thought you might like to know, in case anything comes to mind.
Thanks for helping out.


#14 jimblumberg User is offline

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: writing to a specific line

Posted 12 November 2016 - 04:31 PM

Quote

I'll do some more research to see what I can find out about it, to see if I can get it to write to the line without overwriting the data that's already in the file.

You can only replace or append data in a file but you can't insert data.

Jim


#15 snoopy11 User is offline

Reputation: 1594

  • View blog
  • Posts: 5,010
  • Joined: 20-March 10

Re: writing to a specific line

Posted 12 November 2016 - 09:04 PM

View Postjv1597, on 12 November 2016 - 11:28 PM, said:

By the way, I was getting 7 spaces per line request on what I was doing, the pointer was at 14, for 2 line skip, and at 21 for a 3 line skip. I thought you might like to know, in case anything comes to mind.
Thanks for helping out.

No you weren't....

That would be impossible unless you changed the value of seekp to 7, most likely you were just confusing yourself

and my name is not Skydiver he is someone else.....

look into the ofstream constructor 'mode' settings.

Regards

Snoopy


  • ← Previous Topic
  • C and C++
  • Next Topic →

How to Detect Specific Line and Write to It C++

Source: https://www.dreamincode.net/forums/topic/398863-writing-to-a-specific-line/

0 Response to "How to Detect Specific Line and Write to It C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel