We help IT Professionals succeed at work.

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

tikusbalap
tikusbalap asked
on
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.
Comment
Watch Question

Author

Commented:
One more, i don't want include any .NET Framework classes. Just native please. Compiled one and can run without NET Framework.
Commented:
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

Commented:
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");

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

#include <stdlib.h>
system("copy shibby.txt temp.txt");
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
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

Author

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.

Commented:
vc don't provide fstream.h, it's defined in fstream header file.

modified like this:
#include <fstream>

Commented:
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

Author

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?

Commented:
This page interprets the difference between old and new header files.
http://members.gamedev.net/sicrane/articles/iostream.html

Commented:
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.

Author

Commented:
Thx yuy202 you had provide a great help.

Author

Commented:
I hope you can help sometime later. Thx
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
OK, forget it... I really love how quickly you responded. Keep that up, it'll help you.

Commented:
>>I hope you can help sometime later. Thx
you're welcome, :)

Commented:
>>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.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.