64,652
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main () {
string s;
vector<string> v;
//读
ifstream in("1.txt");
while(in>>s)
v.push_back(s);
in.close();
//写
ofstream out("1.txt");
for(vector<string>::iterator it=v.begin();it!=v.end();++it)
out<<*it<<'\n';
return 0;
}
#include <stdio.h>
#include <string.h>
/*行转化, 返回目标长度,返回0表示本行不用处理,一般是空行,或者空格+换行
返回>0表示转换后的长度,(可能变成多行)*/
int line_transfer(char *src, char *dst, const char *newline)
{
int i = 0;
int j = 0;
while(src[i] == ' ' || src[i] == '\t'){ //去掉行的前导空白
i++;
}
if(src[i] == newline[0]) //此时i==0则是空行,否则是前面一些空格,然后换行,(还是相当于空行)
return 0; //跳过该行继续读下一行
int add_newline = 0;
while(src[i] != 0)
{
if(src[i] == ';' && src[i] != *newline){ //注意如果本身就是;之后跟换行,则不处理,
dst[j++] = src[i++];
strcpy(dst+j, newline); //把换行写进去
j += strlen(newline);
}else if(src[i] == ' ' || src[i] == *newline){
i++;
}else{
dst[j++] = src[i++];
}
}
return j; //转换后j的长度
}
const char *get_newline_str(FILE *fp)
{
char buf[4096];
char *retbuf = fgets(buf, sizeof(buf), fp);
if(retbuf == NULL){
printf("empty file \n");
return NULL;
}
fseek(fp, 0L, SEEK_SET);
int len = strlen(buf);
/*这里假设文件单行不会超过4093字节,不然就有问题
buf[len]是字符串结束符0,fgets函数自动加上去的*/
if(buf[len - 1] == '\n'){
if(buf[len - 2] == '\r'){
return "\r\n";
}
else{
return "\n";
}
}else{
printf("unknown newline format, may be not a txt file\n");
return NULL;
}
}
/*windows 文本文件换行是"\r\n", unix是'\n',
windows环境传入'\r', unix 则传入'\n'*/
int format_file(const char *pathname)
{
char *tmp = "tmp_proc00000";
FILE *fpsrc = fopen(pathname, "r");
FILE *fpdst = fopen(tmp, "a+");
if(!fpsrc || !fpdst){
printf("failed to open file");
return -1;
}
const char *newline_str = get_newline_str(fpsrc);
if(newline_str == NULL){
return -1;
}
char bufr[4096]; //for read
char bufw[4096]; //for write
char *retbuf;
while((retbuf = fgets(bufr, sizeof(bufr), fpsrc)) != NULL)
{
int ret_len = line_transfer(bufr, bufw, newline_str);
if(ret_len > 0){
fwrite(bufw, 1, ret_len, fpdst);
}
}
fclose(fpsrc);
fclose(fpdst);
remove(pathname);
rename(tmp, pathname);
}
void print_file(const char *fname)
{
FILE *fp = fopen(fname, "r");
if(!fp){
printf("failed to open %s\n", fname);
return ;
}
char buf[1024];
char *retbuf;
while((retbuf = fgets(buf, sizeof(buf), fp)) != NULL){
printf("%s", buf);
}
printf("\n");
}
int main()
{
char fname[256];
printf("Enter file name you want to transfer\n");
scanf("%s", fname);
format_file(fname);
print_file(fname);
return 0;
}
#include <stdio.h>
#include <string.h>
/*行转化, 返回目标长度,返回0表示本行不用处理,一般是空行,或者空格+换行
返回>0表示转换后的长度,(可能变成多行)*/
int line_transfer(char *src, char *dst, const char *newline)
{
int i = 0;
int j = 0;
while(src[i] == ' ' || src[i] == '\t'){ //去掉行的前导空白
i++;
}
if(src[i] == newline[0]) //此时i==0则是空行,否则是前面一些空格,然后换行,(还是相当于空行)
return 0; //跳过该行继续读下一行
int add_newline = 0;
while(src[i] != 0)
{
if(src[i] == ';' && src[i] != *newline){ //注意如果本身就是;之后跟换行,则不处理,
dst[j++] = src[i++];
strcpy(dst+j, newline); //把换行写进去
j += strlen(newline);
}else if(src[i] == ' ' || src[i] == *newline){
i++;
}else{
dst[j++] = src[i++];
}
}
return j; //转换后j的长度
}
const char *get_newline_str(FILE *fp)
{
char buf[4096];
char *retbuf = fgets(buf, sizeof(buf), fp);
if(retbuf == NULL){
printf("empty file \n");
return NULL;
}
int len = strlen(buf);
/*这里假设文件单行不会超过4093字节,不然就有问题
buf[len]是字符串结束符0,fgets函数自动加上去的*/
if(buf[len - 1] == '\n'){
if(buf[len - 2] == '\r'){
return "\r\n";
}
else{
return "\n";
}
}else{
printf("unknown newline format, may be not a txt file\n");
return NULL;
}
}
/*windows 文本文件换行是"\r\n", unix是'\n',
windows环境传入'\r', unix 则传入'\n'*/
int format_file(const char *pathname)
{
char *tmp = "tmp_proc00000";
FILE *fpsrc = fopen(pathname, "r");
FILE *fpdst = fopen(tmp, "a+");
if(!fpsrc || !fpdst){
printf("failed to open file");
return -1;
}
const char *newline_str = get_newline_str(fpsrc);
if(newline_str == NULL){
return -1;
}
char bufr[4096]; //for read
char bufw[4096]; //for write
char *retbuf;
while((retbuf = fgets(bufr, sizeof(bufr), fpsrc)) != NULL)
{
int ret_len = line_transfer(bufr, bufw, newline_str);
if(ret_len > 0){
fwrite(bufw, 1, ret_len, fpdst);
}
}
fclose(fpsrc);
fclose(fpdst);
remove(pathname);
rename(tmp, pathname);
}
void print_file(const char *fname)
{
FILE *fp = fopen(fname, "r");
if(!fp){
printf("failed to open %s\n", fname);
return ;
}
char buf[1024];
char *retbuf;
while((retbuf = fgets(buf, sizeof(buf), fp)) != NULL){
printf("%s", buf);
}
printf("\n");
}
int main()
{
char fname[256];
printf("Enter file name you want to transfer\n");
scanf("%s", fname);
format_file(fname);
print_file(fname);
return 0;
}