Ok here is what I want... I want to read the Dictionary file, and output its contents into the dynamic-text.dat file... But it reads the dictionary file and it will not write to the dynamic-text.dat file and I have no clue why... what am i doing wrong.. when I split the code, both files can be opened and written to, but when its like this, they will not read from one and write its contents to the other! =( Also If I altered the program to allow the user to input a string of words into the file, it will only input the first word.. anyone got any ideas?

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;

int main () {
  string line;
  ifstream Dict ("C:/Users/Brandon/Desktop/Programs C++/Dictionary.txt");
  if (Dict.is_open())
  {
      getline (Dict,line);
      Sleep(1000);
            ifstream file ("C:/Users/Brandon/Desktop/test.txt", ios::out | ios::app | ios::binary);
               if (file.is_open())
               {
                 cout<< line <<" ";
               }
               else cout <<"Cannot Open 'File' for writing";
               file.close();
    Dict.close();
  }

  else cout << "Unable to open 'Dict' for reading"; 
  Sleep(5000);
  return 0;
}

why do you have Sleep(...) ?

and normally "if(Dict.is_open()) " is used as a error checker. There is a lot of problems with you code. Instead of listing them all, I will show you a sample program
and you should contrast.

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;

int main () {
  string line;
  ifstream Dict ("C:/Users/Brandon/Desktop/Programs C++/Dictionary.txt"); //file to read
    ifstream file ("C:/Users/Brandon/Desktop/test.txt", ios::out | ios::app | ios::binary); //file to write

  if (!Dict.is_open()) { return -1; /*error */ };
  if (!file.is_open()) { return -1;/ /*error */ };

   while(!Dict.fail()){ //while we can read the file
      getline (Dict,line); //get the current line                   
      cout << line <<" "; //print line to screen
      file << line ; //write line to file
     }
  return 0;
}

I have not compiled this, but you should get an idea.

It would make much more sense to do this instead

ifstream Dict ("C:/Users/Brandon/Desktop/Programs C++/Dictionary.txt"); //file to read
  if (!Dict.is_open()) { return -1; /*error */ };

  ifstream file ("C:/Users/Brandon/Desktop/test.txt", ios::out | ios::app | ios::binary); //file to write
  if (!file.is_open()) { return -1;/ /*error */ };

Why open the test file if the Dict file didn't open?

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;

int main () {
  string line;
  ifstream Dict ("C:/Users/Brandon/Desktop/Programs C++/Dictionary.txt"); //file to read
    ifstream file ("C:/Users/Brandon/Desktop/test.txt", ios::out | ios::app | ios::binary); //file to write

  if (!Dict.is_open()) { return -1; /*error */ };
  if (!file.is_open()) { return -1; /*error */ };

   while(!Dict.fail()){ //while we can read the file
      getline (Dict,line); //get the current line                   
      cout << line <<" "; //print line to screen
      file << line ; //write line to file
     }
  return 0;
}

Its not the error problem for my code... the thing is no matter what i cant get it to write to the file... Im actually trying to write it to a .dat file in binary mode but it wont write for some awkward reason!... I can write strings to the file, but I cant seem to get the Dictionary file to output its contents into the .dat file... aka test.txt


18 C:\Users\Brandon\Desktop\Programs C++\plswork.cpp no match for 'operator<<' in 'file << line'

You're using ifstream instead of ofstream in this line:

ifstream file ("C:/Users/Brandon/Desktop/test.txt", ios::out | ios::app | ios::binary); //file to write

You must also take into account that you won't be able to create files outside of the folder of your executable. You won't even be able to create a folder with plain c++ libraries.

then it should be something like:

ofstream file ("data/test.txt", ios::out | ios::app | ios::binary); //file to write

TIP: output a message after each error check so you can see where it specifically failed.

Edited 6 Years Ago by Isaac Remuant: n/a

#include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <stdio.h>
#include <fstream>
#include <windows.h>
using namespace std;

int main ()
{
 string line;
    ifstream Dict ("C:/dictionary.txt"); //file to read
        getline (Dict,line); //get the current line
        ofstream ipDict;
        ipDict.open ("C:/dynamic-text.dat", ios::out | ios::app | ios::binary);
        ipDict << line <<" ";
        ipDict.close();      
    Dict.close();

return 0;
}

I fixed it like that... it works for me... I wanted to implement a way for users to point the program to their file but ill go search that up now.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.