473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

input output files

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;
}

Oct 24 '05 #1
3 1921
Ko*************@gmail.com wrote:
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-lit....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-lit....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);
i+=1;
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

Oct 24 '05 #2
Ko*************@gmail.com wrote:
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 news: 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

Oct 24 '05 #3
Ko*************@gmail.com wrote:

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;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 24 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer...
5
by: Clemens Park | last post by:
Hi, I am working on a compressor/decompressor for files, and it works great at the moment with text files, but not great all with binary files. The problem is that it reads in binary files as...
7
by: MM | last post by:
Hi there, How can I change my code (below) so that I use an "input argument" to specify the file name of the input file? For example, if I compile the code and that the application then gets the...
1
by: Aalok | last post by:
This is what i want to do. Read a text file as an input and based on that file, Create an outpu text file which saves the contents of the input file in a specifi format in the output file. I...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
2
by: gopala | last post by:
Hi, I am pretty new to python but i do have experience with c++. As a part of learning exercise i wrote a python script to insert beautifying comments(with name, problem...) to our lab program...
1
by: terminatorul | last post by:
Hello Sorry if asking a known question. I have a program that reads lines of text from standard input and writes the non-empty ones back to standard output. In most cases it will be used with...
3
by: sab | last post by:
Hello, I have been working on a python script to parse a continuously growing log file on a UNIX server. The input is the standard in, piped in from the log file. The application works well...
1
by: randysimes | last post by:
I have a program to read in words and sort those words into lexicographical order. The problem I have is that it reads the input file endlessly. I know it does not reach the output section because...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.