+help+ writing to .txt file, trouble with format

Question:
So I want to send and store: words, their definition, synonyms, and part of speech, in a .txt file and later be able to recall and use the words according to their part of speech(and posibly have a synonym picked at random to replace it) ...how might I do that? Examples/explanations would help me a ton!

It's my assuption that I need to write something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  char str[] =userResponse;
  char * pch;
  pch = strtok (userResponse," ");
  ofstream myfile ("dic.txt");
  if (myfile.is_open())
  {
    myfile << strtok


But I'm not sure how to keep it in the format that I want... something along the lines of:
Word 'tab' Parts of speech 'tab' synonyms 'tab' definition

-Sorry if I butchered the code, I'm still fairly new to C++ and coding as a whole.
Last edited on
If you want to use proper C++ read a line (getline()) and then use stringstream to separate each word.

you can always write that as myfile << word << '\t' << synonym << endl;

So you're suggesting to print in format I want write something like,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
  ofstream myfile ("dic.txt");
  if (myfile.is_open())
  {
    myfile <<word<<'\t'<<partsofspeach<<'\t'<<synonyms<<'\t'<<definition<<endl;
  }
  cin<<word<<partsofspeech<<synonyms<<definition<<endl;


Correct?

And then to use (getline()) and stringstream to scan and pull data from dic.txt? How would I tell it what line?

At the moment, that seems to be my biggest hurdel: pulling the correct data, from the correct line, from the file dic.txt
Last edited on
Topic archived. No new replies allowed.