3

I am working on some sort of a notepad in c++. My idea is that user will be able to set a category and tags for each notes. Then he will be save the note and open it, while my program will keep those tags and category. However I am not sure how to exactly save vector to file and then open it so I can have access to it.

if ( infile . is_open() )
{
   while ( !in_file . eof () ) 
   {      
       infile >> category;
   }
}

ofstream f ( filename ) 
if ( f . is_open () )
{
   f << category;
}

f . close ();

The code above works just fine for saving and opening file with category, however I don't know how to do the same with vector of strings for tags. Anybody who could help me out with this would be much appreciated.

J. Donič
  • 53
  • 3
  • 2
    `while ( !in_file . eof () ) ` is a bug: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – drescherjm Jun 01 '19 at 15:17
  • How is the data stored in your file for the vector / How do you plan to separate the strings in your file? – drescherjm Jun 01 '19 at 15:19
  • I am loading each tag into a ```string tag``` and then push it into vector ```tags . pushback ( tag )``` I had to do so, because I am loading in a tag by chars. – J. Donič Jun 01 '19 at 15:23
  • 3
    Do you want the data saved as an unstructured stream of text, an implied (as far as your program is concerned) structured text, or as structured text (e.g., XML, JSON), or as binary? You need to be able to round-trip the data saved-out and loaded-in. You may need to transform strings into an "encoded" string, and when reading it "decode" it back into internal normal form. – Eljay Jun 01 '19 at 15:57
  • 2
    I guess I want it to be non structured. I am already loading my text by lines in ```if ( infile . is_open() ) { while ( !in_file . eof () ) { string tmp; getline ( infile, tmp ); buff -> app_Line ( tmp ); } }``` but the category and tags thing are messing with the code I guess. – J. Donič Jun 01 '19 at 16:03
  • You might consider a `vector` for each line in your text and a `vector>` for each tag where the two ints are the starting and ending indexes into the vector. – doug Jun 01 '19 at 16:13
  • I have no trouble loading lines, I even have them in vector as you suggested. I also can set and then display tags or category without problem. Its just saving and then opening the file where the trouble is. – J. Donič Jun 01 '19 at 16:28

0 Answers0