input output files

K

Kostadis.Spirou

this is a programm that opens config.txt and reads the 2 lines that are
inside it,after that it copies them and i want to put them as
parametres at
ifstream examplefile (file1.data());
ofstream fout (file2.data());
but when i try to run it it doesn't work at all.
any suggestions what to change in my code?


#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
using std::ifstream;

int main()
{
char *word;
char temp_char [200];
string file1,file2;
int long size1;
word = new char [size1];
int i=0;
char * buffer;
long size;

ifstream indata;
int num;
indata.open("config.txt");
if(!indata)
{ cerr << "Error: file could not be opened" << endl;
exit(1);
}

int first_row=true;
while (!indata.eof())
{
while (indata.getline(temp_char,200,'\n'))
{
if (first_row)
{
file1=temp_char;
first_row=false;
//cout<<file1<<endl;
}
else
file2=temp_char;
//cout<<file2<<endl;
}
}
indata.close();

size=1000;
buffer = new char [size];//allocate memory for file content

ifstream examplefile (file1.data());
ofstream fout (file2.data());

if (! examplefile.is_open())
{ cout << "this thing doesn't work "; exit (1); }

while (! examplefile.eof() )
{
examplefile.getline (buffer,size);//get line tou *.txt file
i+=1;
fout << buffer << endl;
}
examplefile.close();
fout.close();
cout <<"the lines of this *.txt are: "<<i<<endl;


}
 
J

Jonathan Mcdougall

this is a programm >

Buy a book! You won't be able to go very far without one. See
www.accu.org.
that opens config.txt and reads the 2 lines that are
inside it,after that it copies them and i want to put them as
parametres at
ifstream examplefile (file1.data());
ofstream fout (file2.data());
but when i try to run it it doesn't work at all.
any suggestions what to change in my code?

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

using std::ifstream;

Redundant if you keep the first one.
int main()
{
char *word;

What is that variable for?
char temp_char [200];
string file1,file2;
int long size1;
word = new char [size1];

Danger! The value of size1 is undefined at this point. Try to output it
to see what I mean. To avoid this problem, always initialize your
variables when you define them:

int long size1 = 0;

And to avoid giving dummy values, always define them near their first
use.
int i=0;
char * buffer;
long size;

C++ is not C. Don't define all your variables at the beginning of the
function.

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.7
ifstream indata;
int num;
indata.open("config.txt");

Make that

std::ifstream indata("config.txt");

And "num" is unused.
if(!indata)
{ cerr << "Error: file could not be opened" << endl;
exit(1);
}

int first_row=true;
while (!indata.eof())

eof is not set until a read is done. Change the loop to

while (true)
{
// read something
// check for eof, break if true
}
{
while (indata.getline(temp_char,200,'\n'))

Don't use std::istream::getline, use std::getline which works with
std::string:

std::string s;
getline(indata, s);

std::cout << "The line I got: " << s;
{
if (first_row)
{
file1=temp_char;
first_row=false;
//cout<<file1<<endl;
}
else
file2=temp_char;
//cout<<file2<<endl;

You are using a loop for reading two values? Come on!
}
}
indata.close();

Not necessary.
size=1000;

First use of size, so that should be a definition:

long size=1000;
buffer = new char [size];//allocate memory for file content

Who is going to delete this? Nobody, because you forgot to do it. When
you are done with buffer, do

delete[] buffer;
ifstream examplefile (file1.data());
ofstream fout (file2.data());

if (! examplefile.is_open())
{ cout << "this thing doesn't work "; exit (1); }

Use

if (!examplefile)

instead
while (! examplefile.eof() )

Again, the only way to detect eof is to read first.
{
examplefile.getline (buffer,size);//get line tou *.txt file

Again, use std::getline(stream, string);

Prefer ++i;
fout << buffer << endl;
}
examplefile.close();
fout.close();
Unnecessary.

cout <<"the lines of this *.txt are: "<<i<<endl;


The links I provided are from the C++ faq. Read it.
http://www.parashift.com/c++-faq-lite


Jonathan
 
M

mlimber

this is a programm that opens config.txt and reads the 2 lines that are
inside it,after that it copies them and i want to put them as
parametres at
ifstream examplefile (file1.data());
ofstream fout (file2.data());
but when i try to run it it doesn't work at all.
any suggestions what to change in my code?


#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
using std::ifstream;

This line is unnecessary since you have using namespace std;
int main()
{

Suggestion: don't declare the variables until you need them, and
declare them in the smallest possible scope.
char *word;
char temp_char [200];
string file1,file2;
int long size1;
word = new char [size1];

This is bad you didn't initialize size1!
int i=0;
char * buffer;
long size;

ifstream indata;
int num;
indata.open("config.txt");

Prefer opening via the constructor:

ifstream indata( "config.txt" ); // No need for indata.open()
if(!indata)
{ cerr << "Error: file could not be opened" << endl;
exit(1);
}

int first_row=true;

Prefer:

bool first_row = true;
while (!indata.eof())
{
while (indata.getline(temp_char,200,'\n'))

These two while loops could be collapsed into one:

while( indata.getline(temp_char,200,'\n') )

The check for eof won't detect other failures, so it's best to use this
form. Also, consider using std::string instead of a raw character
array:

string str;
while( getline( indata, str ) )
{
if (first_row)
{
file1=temp_char;
first_row=false;
//cout<<file1<<endl;
}
else
file2=temp_char;
//cout<<file2<<endl;
}
}
indata.close();

This close will be done automatically by the destructor, but you can do
it here if desired.
size=1000;
buffer = new char [size];//allocate memory for file content

ifstream examplefile (file1.data());
ofstream fout (file2.data());

Prefer:

ifstream examplefile( file1.c_str() );
ofstream fout( file2.c_str() );
if (! examplefile.is_open())
{ cout << "this thing doesn't work "; exit (1); }

while (! examplefile.eof() )

See above on eof.
{
examplefile.getline (buffer,size);//get line tou *.txt file
i+=1;
fout << buffer << endl;
}
examplefile.close();
fout.close();

See above on automatic closure.
cout <<"the lines of this *.txt are: "<<i<<endl;


}

Cheers! --M
 
K

Karl Heinz Buchegger

this is a programm that opens config.txt and reads the 2 lines that are
inside it,after that it copies them and i want to put them as
parametres at
ifstream examplefile (file1.data());
ofstream fout (file2.data());
but when i try to run it it doesn't work at all.
any suggestions what to change in my code?

Yep. Throw it away. Write a new one.
But this time:
use std::string for string manipulation.
use loops where you need them
use the correct C++ read-loop idiom to read files.
don't do dynamic allocation yourself, if you don't have to

eg.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream indata( "config.txt" );

if( !indata ) {
cout << "Failed to open input file 'config.txt'" << endl;
return EXIT_FAILURE;
}

string File1;
if( !getline( indata, File1 ) ) {
cout << "Failed to read name of first file from 'config.txt'\n";
return EXIT_FAILURE;
}

string File2;
if( !getline( indata, File2 ) ) {
cout << "Failed to read name of second file from 'config.txt'\n";
return EXIT_FAILURE;
}


ifstream InFile( File1.c_str() );

if( !InFile ) {
cout << "Failed to open input file '" << File1 << "'\n";
return EXIT_FAILURE;
}

ofstream OutFile( File2.c_str() );

if( !OutFile ) {
cout << "Failed to open output file '" << File2 << "'\n";
return EXIT_FAILURE;
}

long Lines = 0;
string Buffer;
while( getline( InFile, Buffer ) ) {
OutFile << Buffer << "\n";
Lines++;
}

cout << "Copied " << Lines << " Line(s) from '" << File1 << "' to '" << File2 << "'\n";

return EXIT_SUCCESS;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top