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("co nfig.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<<en dl;
}
else
file2=temp_char ;
//cout<<file2<<en dl;
}
}
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.get line (buffer,size);//get line tou *.txt file
i+=1;
fout << buffer << endl;
}
examplefile.clo se();
fout.close();
cout <<"the lines of this *.txt are: "<<i<<endl;
} 3 1937 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("co nfig.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::g etline, 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<<en dl; } else file2=temp_char ; //cout<<file2<<en dl;
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.get line (buffer,size);//get line tou *.txt file
Again, use std::getline(st ream, string);
i+=1;
Prefer ++i;
fout << buffer << endl; } examplefile.clo se(); 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 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("co nfig.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<<en dl; } else file2=temp_char ; //cout<<file2<<en dl; } } 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.get line (buffer,size);//get line tou *.txt file i+=1; fout << buffer << endl; } examplefile.clo se(); fout.close();
See above on automatic closure.
cout <<"the lines of this *.txt are: "<<i<<endl;
}
Cheers! --M 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
http://alexvn.freeservers.com/s1/perfometer.html
|
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 text files, therefore
resulting in incorrect compression(it will compress 0.x% instead of around
97% for a certain file).
I am wondering if there is a certain function for cin that allows to
recognize an input file as binary. I am working in a UNIX...
|
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 name "splitdata", then I want to be
able to call my application with something like this (datafile.txt will then
be the name of the input file):
splitdata datafile.txt
|
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 know how to read a text file and i can even display the contents o
the input file on the prompt. However i am trying to figure out how t
save the contents of the input file in a specific manner.
|
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 returning null
pointers.) It looks pretty watertight to me, but my version of lint
complains about use of deallocated pointers, etc. Is this code
completely safe on all input, or have I missed something?
/* Header files included in the program...
| |
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 learned
file input/output very well (I self taught all the programming I
know...and my c++ book was not good) and so I'm not sure what's wrong
with this. The problem is the function void encodemsg(fstream *img,
fstream *msg, fstream *out, char key)
...
|
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 source
files (c language).
The script is working for most of the cases. The problem is i also
need to append the test output of the lab programs to the
corresponding source files(again the output should be added as a
beautified comment). The lab...
|
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 standard input and output streams
redirected to FS files.
If my input is wide-character (UNICODE) I would like to use wcin and
wcout and write wide-character text to standard output. If my input is
|
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 for the most part, but
the problem is when attempting to continuously pipe information into
the application via the tail -f command. The command line looks
something like this:
tail -f <logfile| grep <search string| python parse.py
|
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 the output file is not created.
size_t maxArraySize = 1000;
int main (int argc, char* argv)
{
char* a ;
unsigned int count = 0;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |