C to C++ question
Oct 22, 2009 at 7:01pm UTC
What is the best way to exactly translate the following C code to C++?:
1 2
char d;
while ((d = fgetc("test.txt" )) != EOF)
I've tried the following but it didn't work:
1 2 3
char d;
ifstream infile("test.txt" );
while ((!infile.get(d).eof()))
Any suggestions? Thx!
Oct 22, 2009 at 7:33pm UTC
Oct 22, 2009 at 7:41pm UTC
your right, tried to simplify it for the forum :/
Original code:
1 2 3 4 5 6
char d;
char *fileName;
FILE *fileDocument;
fileDocument = fopen(fileName,"r" );
while ((d = fgetc(fileDocument)) != EOF){
...
is it the same with ifstream infile(), does it only works with a FILE pointer?
Last edited on Oct 22, 2009 at 7:47pm UTC
Oct 22, 2009 at 7:49pm UTC
here is a program i had to make in my csci 1301 lab. Basically you have two files one called input.txt and one called output.txt. you will put some words in the input.txt file and then this will encrypt it and write the encryption to the blank output file. and the bottom main will decrypt it by the user entering the output.txt and it will desplay the decrypted message. I have only learned c++. Im in the second part of it now so hope this helps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void mygetline(istream& is, string &str);
void main()
{
string filename;
cout << "Enter filename" ;
mygetline(cin, filename);
ifstream in_file(filename.c_str());
if (in_file.is_open()==false )
{
cout << "\a Error: could not open file." ;
system("pause" );
exit(-1);
}
ofstream out_file("output.txt" );
char ch;
while (in_file >> ch)
{
ch = ch + 13;
out_file << ch;
}
in_file.close();
out_file.close();
system("pause" );
}
void mygetline(istream& is, string &str)
{
str = "" ; // remove old contents
if (is != cin && is.peek() == '\n' ) // skip to next line if needed
is.get();
char ch;
while ((ch=is.get()) != '\n' && ch != EOF) // keep reading chars until
str += ch; // end of line or file reached
}
decrypt main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void mygetline(istream& is, string &str);
void main()
{
string filename;
cout << "Enter filename: " ;
mygetline(cin, filename);
ifstream in_file(filename.c_str());
if (in_file.is_open()==false )
{
cout << "\a Error: could not open file." ;
system("pause" );
exit(-1);
}
//ofstream out_file("output.txt");
char ch;
while (in_file >> ch)
{
ch = ch - 13;
cout << ch;
}
cout << endl << endl << endl;
in_file.close();
system("pause" );
}
void mygetline(istream& is, string &str)
{
str = "" ; // remove old contents
if (is != cin && is.peek() == '\n' ) // skip to next line if needed
is.get();
char ch;
while ((ch=is.get()) != '\n' && ch != EOF) // keep reading chars until
str += ch; // end of line or file reached
}
Oct 23, 2009 at 10:32am UTC
Alright I made some progress thx to btripp. My next problem is that I need to print the linked list I made with my ifstream. The output I'm getting now is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
file to read from opened
file to write to opened
This
is
the
text
that
should
be
in
the
linked
list.
No. of elements = 11
This
is
the
text
that
should
be
in
the
linked
{EDIT} Le crap, I deleted newword before displaying my linked list, fixed! See below for my next question ;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
int main()
{
linklist ll;
string word;
char *newword;
ifstream infile("test.txt" );
if (infile.is_open())
{
cout << "file to read from opened" << endl;
}
else
{
cout << "unable to open file, incorrect or no 'input' argument..." << endl;
}
ofstream outfile("out.txt" );
if (outfile.is_open())
{
cout << "file to write to opened" << endl;
//outfile.close();
}
else
{
cout << "unable to write file, incorrect or no 'output' argument..." << endl;
}
while (!infile.eof())
{
getline(infile, word, ' ' );
cout << word << endl;
outfile << word << "\n" ;
newword = new char [word.size()+1];
strcpy (newword, word.c_str());
ll.append(newword);
}
cout<<"No. of elements = " <<ll.count();
delete [] newword;
infile.close();
outfile.close();
ll.display();
return 0;
}
and these are my classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#include <iostream>
#include <fstream.h>
#include <cstring>
#include <string.h>
using namespace std;
class linklist
{
private :
struct node
{
char *data;
node *link;
}*p;
public :
linklist();
void append( char *txt );
void display();
int count();
int isChar(char c);
~linklist();
};
linklist::linklist()
{
p=NULL;
}
int linklist::isChar(char c) {
if ((c >= 'a' ) && (c <= 'z' ) || (c>='A' ) && (c<='Z' ) || c == 39) {
return 1;
}
else
{
return 0;
}
}
void linklist::append(char *txt)
{
node *q,*t;
if ( p == NULL )
{
p = new node;
p->data = txt;
p->link = NULL;
}
else
{
q = p;
while ( q->link != NULL )
q = q->link;
t = new node;
t->data = txt;
t->link = NULL;
q->link = t;
}
}
void linklist::display()
{
node *q;
cout<<endl;
for ( q = p ; q != NULL ; q = q->link )
cout<<endl<<q->data;
}
int linklist::count()
{
node *q;
int c=0;
for ( q=p ; q != NULL ; q = q->link )
c++;
return c;
}
linklist::~linklist()
{
node *q;
if ( p == NULL )
return ;
while ( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}
I'm trying to lose every punctuation :P, wich doesn't work now cuz of my getline(infile, word, ' ');. Any ideas?
Thx again!
Last edited on Oct 23, 2009 at 10:35am UTC
Topic archived. No new replies allowed.