首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

文件的读写解决办法

2012-04-01 
文件的读写哪位仁兄能给个例子,是关于对文本文件进行读写操作例子。写文件一定要能够换行续写的,谢谢了,急

文件的读写
哪位仁兄能给个例子,是关于对文本文件进行读写操作例子。写文件一定要能够换行续写的,谢谢了,急急急!!!!

[解决办法]
#include <stdio.h>

int main(void)
{
FILE *in, *out;

if ((in = fopen( "TESTFILE.DAT ", "rt "))
== NULL)
{
fprintf(stderr, "Cannot open input file.\n ");
return 1;
}

if ((out = fopen( "TESTFILE.BAK ", "wt "))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n ");
return 1;
}

while (!feof(in))
fputc(fgetc(in), out);

fclose(in);
fclose(out);

return 0;
}
[解决办法]
写文件:

#include <stdio.h>
#include <string.h>

void main(void)
{
FILE *file;
char *path= "d:\\text.txt ";
char *buffer= "testtesttest ";
file=fopen(path, "wb ");
fwrite(buffer,sizeof(char),strlen(buffer),file);
}
[解决办法]
续写:

#include <stdio.h>
#include <string.h>

void main(void)
{
FILE *file;
char *path= "d:\\text.txt ";
char *buffer= "\r\ntesttesttest ";
file=fopen(path, "ab+ ");
fwrite(buffer,sizeof(char),strlen(buffer),file);
fclose(file);
}
[解决办法]
持续写也可以的
先用StringList的LoadFromFile把log.txt里的内容读到一个StringList里
在把新的日志信息加到这个TStringList里
最后在把这个TStringList-> SaveToFile
[解决办法]
读:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main(void)
{
FILE *file;
char *path= "d:\\text.txt ";
file=fopen(path, "rb ");
fseek(file,0,SEEK_END);
int length=ftell(file);
fseek(file,0,SEEK_SET);
char *buffer=(char *)malloc(length);
fread(buffer,sizeof(char),length,file);
printf( "%s\n ",buffer);
fclose(file);
}
[解决办法]
bool TForm1::DealWith(String filename)
{
ifstream input;
input.open(filename.c_str(), ios::in);
if(!input)
{
ShowMessage( "文件未被打开 ");
return false;
}
ofstream output;
String outname = filename.SubString(1, filename.LastDelimiter( ". ") - 1 );
outname = outname + "_result.bin ";
output.open(outname.c_str(), ios::out);
if(!output)
{
ShowMessage( "文件未被打开 ");
return false;
}
string strtemp = " ";
while(!input.eof())
{
strtemp = " ";
getline(input,strtemp, '\n ');
if(strtemp == " ")
{
continue;
}
vector <wordnum> v_word;
wordnum wordnumtemp;
v_word.clear();
string texttemp = strtemp;
int pos = strtemp.find( '| ');


int pos1 = 0;
while(pos != -1)
{
wordnumtemp.content = strtemp.substr(0, pos);
if(wordnumtemp.content.operator [](0) == '[ ')
{
pos1 = wordnumtemp.content.find( '] ');
wordnumtemp.partofspeech = wordnumtemp.content.substr(0, pos1 + 1);
wordnumtemp.content = wordnumtemp.content.substr(pos1 + 1, wordnumtemp.content.length() - pos1 );
}
else
{
wordnumtemp.partofspeech = " ";
}
//if(wordnumtemp.partofspeech == " ")
// {
// v_word.push_back(wordnumtemp);
// }
// else
// {
int i = 1;
bool bcheckword = false;
for(; i < v_word.size(); i++)
{
if(v_word.at(i).partofspeech == wordnumtemp.partofspeech)
{
v_word.at(i).content = v_word.at(i).content + "; " + wordnumtemp.content;
bcheckword = true;
break;
}
}
if(!bcheckword)
{
v_word.push_back(wordnumtemp);
}
//}
strtemp = strtemp.substr(pos + 1, strtemp.length() - pos);
pos = strtemp.find( '| ');
}
if(strtemp != " ")
{
wordnumtemp.content = strtemp;
if(wordnumtemp.content.operator [](0) == '[ ')
{
pos1 = wordnumtemp.content.find( '] ');
wordnumtemp.partofspeech = wordnumtemp.content.substr(0, pos1 + 1);
wordnumtemp.content = wordnumtemp.content.substr(pos1 + 1, wordnumtemp.content.length() - pos1 );
}
else
{
wordnumtemp.partofspeech = " ";
}
//if(wordnumtemp.partofspeech == " ")
//{
// v_word.push_back(wordnumtemp);
//}
//else
//{
int i = 1;
bool bcheckword = false;
for(; i < v_word.size(); i++)
{
if(v_word.at(i).partofspeech == wordnumtemp.partofspeech)
{
v_word.at(i).content = v_word.at(i).content + "; " + wordnumtemp.content;
bcheckword = true;
break;
}


}
if(!bcheckword)
{
v_word.push_back(wordnumtemp);
}
//}
}
string sVertical = "| ";
if(v_word.size() > = 1)
{
wordnumtemp = v_word.at(0);
output < < wordnumtemp.content < <sVertical;

}

热点排行