How to manipulate string, writing them to the file, copying and delete after finish?

Hi expert,

This is maybe a simple for question for you. But I'm new to this language, so please help me giving the sample for these case:

0. Create a console application,
1. Reading some string from a file
2. Manipulate the string, insert a current datetime to the last string
3. Writing back to the file
4. Copying the file to another location
5. Delete after successfully copy.

I hope this step would be in one compiled console app. Would be better if you include step in opening project in vs2008 c++.

Thank you very much.
LVL 6
tikusbalapAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tikusbalapAuthor Commented:
One more, i don't want include any .NET Framework classes. Just native please. Compiled one and can run without NET Framework.
0
yuy2002Commented:
You can use c++ standary lib which supported by all compilers.
1. open a file


#include <fstream.h>
void main()
{
                fstream file;
      
      file.open("file.ext",iso::in|ios::out)
      
      //do an input or output here
      
      file.close();
}
2. read strings

char output[100];

file>>output;

Usages:

      file>>char *;            //ie an array
      file>>char;                  //single char
      file.get(char);            //single char
      file.get(char *,int);      //read a string
      file.getline(char *,int sz);
      file.getline(char *,int sz,char eol);

3. insert a current datetime to the last string

#include <string.h>
#include <time.h>
//get the current time
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep);
//add time format:2000/1/1 11:11:11
sprintf(output,"%s%d/%d/%d %d:%d:%d",output,(1900+p->tm_year),(1+p->tm_mon),p->tm_mday,p->tm_hour,p->tm_min, p->tm_sec);


4. write back to the file
file<<output;

5. copy file, delete file
If your app runs on win system, use "ShellExecute" to execute dos command.

HINSTANCE ShellExecute(
    HWND hwnd,
    LPCTSTR lpOperation,
    LPCTSTR lpFile,
    LPCTSTR lpParameters,
    LPCTSTR lpDirectory,
    INT nShowCmd
);


Hope it helpful,
charles
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yuy2002Commented:
Another method:
you could copy the file out stream to the new file instead of copy file.

1.create another new file which will copy to

      fstream copyfile;
      copyfile.open("copyfile.ext",iso::in)
     
      //assuming output contained file contents
      copyfile<<output

2.remove original file.
   #include <stdio.h>
   remove("./file.ext");
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

yuy2002Commented:
In step 5, copy and delete file.
use "system" in stdlib.h is also recommended.

#include <stdlib.h>
system("copy shibby.txt temp.txt");
0
jkrCommented:
You can just do that like


#include <fstream.h>
#include <string>
#include <time.h>
 
using namespace std;
 
void read_file (const string& name, stringt& line) {
 
    ifstream in(name.c_str());
 
    getline(in,line);
}
 
void  write_file (const string& name, stringt& line) {
 
    ofstream out(name.c_str(), ios_base::trunc);
 
    struct tm   *newTime;
    time_t      szClock;
 
    // Get time in seconds
    time( &szClock );
 
    // Convert time to struct tm form 
    newTime = localtime( &szClock );
 
    line += asctime( newTime ); // Add local time as a string.
 
    out << line << endl;
}
int main ()
{
    string line;
    string strFile = "test.txt";
 
    read_file(strFile, line);
 
    write_file(strFile, line);
 
    return 0;
}

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
0
rstaveleyCommented:
With reference to 4, a very efficient way to copy files is to use streambuf iterators with the standard library copy algorithm.
#include <fstream>
#include <iterator>
#include <algorithm>
 
// Copy a file
bool FileCopy(const char *SourceFilename, const char *DestinationFilename)
{
	std::ifstream fin(SourceFilename);
	if (!fin) return false;
	std::ofstream fout(DestinationFilename);
	if (!fout) return false;
	copy(std::istreambuf_iterator<char>(fin),std::istreambuf_iterator<char>(),std::ostreambuf_iterator<char>(fout));
	return true;
}

Open in new window

0
tikusbalapAuthor Commented:
Sorry, i'm too stupid :). I open vs2008 open console project Win32Application for cpp. Then i paste one of sample code above, then i getting this error.

fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory

What step i'm missing?

Thank you.
0
yuy2002Commented:
vc don't provide fstream.h, it's defined in fstream header file.

modified like this:
#include <fstream>
0
yuy2002Commented:
fstream.h is used in old c++ standard, which supported by vs6.0.
fstream is declared for new c++ standard,which supported by vs.2005 and later.

http://msdn2.microsoft.com/en-us/library/k8w40w6t(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/a7tkse1h(VS.80).aspx

0
tikusbalapAuthor Commented:
Thx, but one more please if you don't mind.

How i execute another application in cpp, say i want to execute notepad or other custom app?
0
yuy2002Commented:
This page interprets the difference between old and new header files.
http://members.gamedev.net/sicrane/articles/iostream.html
0
yuy2002Commented:
What you want is to run another programme in this process?
You could use "system"  in stdlib.h(process.h for win) to run another exe programe.
0
tikusbalapAuthor Commented:
Thx yuy202 you had provide a great help.
0
tikusbalapAuthor Commented:
I hope you can help sometime later. Thx
0
jkrCommented:
Stupid question:
#include <fstream.h>
#include <string>
#include <time.h>
 
using namespace std;
 
void read_file (const string& name, stringt& line) {
 
    ifstream in(name.c_str());
 
    getline(in,line);
}
 
void  write_file (const string& name, stringt& line) {
 
    ofstream out(name.c_str(), ios_base::trunc);
 
    struct tm   *newTime;
    time_t      szClock;
 
    // Get time in seconds
    time( &szClock );
 
    // Convert time to struct tm form 
    newTime = localtime( &szClock );
 
    line += asctime( newTime ); // Add local time as a string.
 
    out << line << endl;
}
int main ()
{
    string line;
    string strFile = "test.txt";
 
    read_file(strFile, line);
 
    write_file(strFile, line);
 
    return 0;
}
 
// what does not work for you here or what are you having trouble with?

Open in new window

0
jkrCommented:
OK, forget it... I really love how quickly you responded. Keep that up, it'll help you.
0
yuy2002Commented:
>>I hope you can help sometime later. Thx
you're welcome, :)
0
yuy2002Commented:
>>OK, forget it... I really love how quickly you responded. Keep that up, it'll help you.

Thanks for your praise,  i'll do my best in my spare time.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.