I need to get characters one at a time from an ofstream because I need to pass the characters into a function. This is what I was trying but it says "ofstream has no member named get".
Here is the code I am using.

ofstream outFile("source.txt", ios::out);

   a1.prepare(inFile2, outFile);

   ifstream inFile3("encrypted.txt", ios::in);
   char ch;
   while(outFile.get(ch))
   {
   a1.encode(ch);
   }

Thanks in advance for any advice.

you should use an instance of ifstream to get chars from a file. ofstream is used for writing and indeed it does'nt have a "get" member.

you could also use an interator like this
ifstream in("file.***");
istream_iterator<char> input(in);

or

ifstream in("file.***");
in.get(char);

like you already tried but using the ifstream stream for input.

you should use an instance of ifstream to get chars from a file. ofstream is used for writing and indeed it does'nt have a "get" member.

you could also use an interator like this
ifstream in("file.***");
istream_iterator<char> input(in);

or

ifstream in("file.***");
in.get(char);

like you already tried but using the ifstream stream for input.

Maybe I should give more information. I have already taken an infile stream and made all the characters and put them and spaces only it into my outfile stream. Now I am suppose to do a cipher on the text. I am suppose to do this in another function that takes a char as its parameter. So I have to take what I have in the outfile stream and send it one character at a time to the function. The function will then return one character which I am suppose to put into a different text file for more processing.

:) ok have it your way.. but this worked for me

#include <iostream>
#include <fstream>

using namespace std;

void put(const char*);
void get(char&);

int main ()
 {
   char a;
   put("a");
   get(a);
   cout << a;
 }

void put(const char* c)
  {
    streamsize num=1;    
    ofstream out ("file.txt");    
    out.write(c,num);
    out.close();
  }

void get(char& c)
  {
    ifstream in("file.txt");
    in.get(c);
  }

Edited 6 Years Ago by Nick Evan: Added code-tags

I guess I don't understand how to get things from infile to outfile streams. Not trying to argue with you.

The main reason your program fails is because you are trying to read from your output file. You should be reading from your input file instead.

ofstream outFile("source.txt", ios::out);
 
   a1.prepare(inFile2, outFile);
 
   ifstream inFile3("encrypted.txt", ios::in);
   char ch;
   while(inFile3.get(ch))
   {
   a1.encode(ch);
   }

As far as I can tell that doesn't work I tried it and tried to print out
inFile3 but there is nothing in it.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include "code.h"

using namespace std;

int main()
{
   ifstream inFile("cipher.txt", ios::in);
   if (!inFile)
   {
      cout << "Failed to open cipher.txt file. \n";
      exit (1);
   }

   char array[26];

   for (int i=0; i<26;i++)
   {
      inFile >> array[i];
   }


   Code a1(array);


   ifstream inFile2("raw.txt", ios::in);
   if (!inFile2)
   {
      cout << "Failed to open raw.txt file. \n";
      exit (1);
   }

   char t;
   t=inFile2.get();
 while (inFile2)
   {
      cout << t;
      t=inFile2.get();
   }

   cout << "That was text before being prepared. \n";

   ofstream outFile("source.txt", ios::out);
   if (!outFile)
   {
      cout << "Failed to open source.txt. \n";
   }

   a1.prepare(inFile2, outFile);
   //this outFile has to be where the characters to encode come from
   //otherwise inFile3 is just empty??


   ifstream inFile3("encrypted.txt", ios::in);
   char ch;
   while(inFile3.get(ch))
   {
      a1.encode(ch);
   }

   char c;
   c=inFile3.get();
   while(inFile3)
   {
      cout << c;
      c=inFile3.get();
   }

I am still stumped on how to get the ofstream stuff into a ifstream stream I can use.

Can your program be run successfully? I do not have a compiler with me now. But judging from the code, your program might be running in an infinite loop; stuck at line 40 of your code.

char t;
   t=inFile2.get();
 while (inFile2)
   {
      cout << t;
      t=inFile2.get();
   }

The checking in the while loop always return true since the pointer is not equal to null. If so, then your encrypted.txt will not have anything. If this is not the problem. Then the problem is in line 67. The first loop of getch() for inFile3 already makes the pointer pointing to the end of file.

ifstream inFile3("encrypted.txt", ios::in);
   char ch;
   while(inFile3.get(ch))
   {
      a1.encode(ch);
   }

Thus, nothing will be retrieved when you try reading from it again.

char c;
   c=inFile3.get();
   while(inFile3)
   {
      cout << c;
      c=inFile3.get();
   }

Before the second attempt to read from inFile3, use seekg() to reset file pointer to the beginning of file. Here is an explanation for the function. http://www.cplusplus.com/reference/iostream/istream/seekg/

You can't get something from an output stream ...

Do you want to read a single character at a time from a file, crypt it and then put it back on a new file?

Edited 6 Years Ago by vidit_X: n/a

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