一个文件操作题目。急急,万分感谢
从一个文件中读数据,写入另一文件。要对该文件每一行排序,
要求每行第一个字符小的要靠前。
问题点数:100、回复次数:11Top
1 楼piaochen_2002(执子之手,与子偕老!)回复于 2006-03-09 10:39:53 得分 0
用mapTop
2 楼yangvxin1(小杨)回复于 2006-03-09 10:47:22 得分 0
假如文件只有10行,每行10个字符.怎么写这个程序呀?给点源码提示Top
3 楼Squall1009(钰枫)(找工作ing)回复于 2006-03-09 10:56:31 得分 0
用C++的stl的很方便Top
4 楼yangvxin1(小杨)回复于 2006-03-09 10:57:56 得分 0
用c语言Top
5 楼msjhonlili(万里长城永不倒,千里黄河水滔滔!)回复于 2006-03-09 11:03:15 得分 0
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void sort(string& src)
{
}
int main(int argc, char** argv)
{
ifstream infile("test.txt");
ofstream out;
string strBuf;
while(!infile.eof())
{
getline(infile, strBuf, '\n'); // 读一行数据到strBuf
sort(strBuf);
out << strBuf << endl;
}
return 0;
}Top
6 楼msjhonlili(万里长城永不倒,千里黄河水滔滔!)回复于 2006-03-09 11:07:40 得分 0
忘加
infile.close();
out.close();Top
7 楼lovexpshl(白浪)回复于 2006-03-09 12:18:19 得分 30
//==========================================================
// Test01009.cpp
// 从一个文件中读取数据,写入到另一个文件中,按行首字母排序
//==========================================================
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
//-----------------------------------------
#define LINELENGTH 255
//-----------------------------------------
const char *strIFileName = "in.txt";
const char *strOFileName = "out.txt";
//-----------------------------------------
bool InputTxt(vector <char *> &vs);
bool OutputTxt(vector <char *> &vs);
void Process(vector <char *> &vs);
//-----------------------------------------
int main(int argc, char* argv[])
{
//定义存放行数据的向量
vector <char *> vs;
//读取文件信息
if (!InputTxt(vs))
{
cout<<"Open input file failed."<<endl<<endl;
return -1;
}
//按首字母排序
Process(vs);
//输出文件信息
if (!OutputTxt(vs))
{
cout<<"Open output file failed."<<endl<<endl;
return -1;
}
return 0;
}
//-----------------------------------------
bool InputTxt(vector <char *> &vs)
{
//-------------- 定义变量
char *str = new char[LINELENGTH];
ifstream in(strIFileName);
//-------------- 判断文件是否存在
if (!in) return false;
//-------------- 读取文件信息
cout<<"---------------------------------"<<endl;
cout<<strIFileName<<":"<<endl;
cout<<"---------------------------------"<<endl;
while (in.getline(str, LINELENGTH))
{
cout<<str<<endl;
vs.push_back(str);
str = new char[LINELENGTH];
}
cout<<endl<<endl;
in.close();
return true;
}
//-----------------------------------------
bool OutputTxt(vector <char *> &vs)
{
//-------------- 定义变量
int i;
ofstream out(strOFileName);
//-------------- 判断文件是否存在
if (!out) return false;
//-------------- 输出并打印文件信息
cout<<"---------------------------------"<<endl;
cout<<strOFileName<<":"<<endl;
cout<<"---------------------------------"<<endl;
for (i=0; i<vs.size(); i++)
{
cout<<vs[i]<<endl;
out<<vs[i]<<endl;
delete vs[i];
vs[i] = NULL;
}
cout<<endl;
vs.clear();
out.close();
return true;
}
//-----------------------------------------
void Process(vector <char *> &vs)
{
//-------------- 定义变量
char *tmp;
int i;
int j;
//-------------- 排序
for (i=0; i<vs.size(); i++)
{
for (j=i+1; j<vs.size(); j++)
{
if (*vs[i] > *vs[j])
{
tmp = vs[i];
vs[i] = vs[j];
vs[j] = tmp;
}
}
}
}Top
8 楼lovexpshl(白浪)回复于 2006-03-09 12:32:01 得分 0
刚忘了!
要去掉
#include "stdafx.h"
#include <string>Top
9 楼Squall1009(钰枫)(找工作ing)回复于 2006-03-09 12:42:20 得分 30
------------filesort-------
#include <stdio.h>
#include <string.h>
struct node
{
char * str;
struct node * next;
};
int cmpnode(struct node * n1, struct node * n2)
{
return strcmp(n1 -> str, n2 -> str);
};
void add(struct node * start, struct node * node)
{
if(start -> next == NULL)
{
start -> next = node;
return;
}
struct node * before = start;
struct node * now = start -> next;
while(now != NULL)
{
if(cmpnode(now, node) >= 0)
break;
before = now;
now = now -> next;
}
before -> next = node;
node -> next = now;
};
void freelist(struct node * start)
{
struct node * now = start -> next;
while(now != NULL)
{
free(now -> str);
struct node * tmp = now;
now = now -> next;
free(tmp);
}
};
int main(int argc, char * argv[])
{
if(argc != 3)
{
printf("filesort fromfilename tofilename\nto use\n");
return -1;
}
struct node first = {NULL, NULL};
struct node * start = &first;
struct node * dis = NULL;
FILE * fp = fopen(argv[1], "r");
if(!fp)
{
printf("file %s not exists\n", argv[1]);
return -1;
}
char line[8096];
memset(line, 0, 8096);
fgets(line, 8096, fp);
while(line[0] != 0 && line[0] != '\n')
{
struct node * tmp = (struct node *) malloc(sizeof(struct node));
tmp -> str = (char *) malloc(strlen(line) + 1);
strncpy(tmp -> str, line, strlen(line) + 1);
if((tmp -> str)[strlen(line) - 1] == '\n');
(tmp -> str)[strlen(line) - 1] = 0;
tmp -> next = NULL;
add(start, tmp);
memset(line, 0, 8096);
fgets(line, 8096, fp);
}
fclose(fp);
FILE * out = fopen(argv[2],"w");
dis = start -> next;
while(dis!= NULL)
{
fputs(dis -> str,out);
fputc('\n',out);
dis = dis -> next;
}
fclose(out);
freelist(start);
system("pause");
return 0;
}
编译成filesort,然后用filesort 你要排序的文件名 生成的文件名
就可以运行了Top
10 楼duduhaha(三人行必有我师)回复于 2006-03-09 14:21:41 得分 40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char *p, int length);
int main(void)
{
char s[50];
FILE *fp1;
FILE *fp2;
int len;
fp1 = fopen("a.txt","r");
if(fp1 == NULL)
exit(-1);
fp2 = fopen("b.txt","w");
if(fp2 == NULL)
exit(-1);
while(fgets(s,sizeof(s),fp1) != NULL)
{
len = strlen(s);
sort(s, len - 1);
fputs(s,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
void sort(char *p, int length)
{
int i;
int j;
char min;
int k;
for(i = 0; i < length; i++)
{
min = p[i];
for(j = i + 1; j < length; j++)
{
if(p[j] < min)
{
min = p[j];
k = j;
}
}
p[k] = p[i];
p[i] = min;
}
}Top
11 楼duduhaha(三人行必有我师)回复于 2006-03-09 15:42:54 得分 0
从一个文件中读数据,写入另一文件。要对该文件每一行排序,
要求每行第一个字符小的要靠前。
好像理解错了,是只按首字母排序?而不是对10个字符串排序吧.Top




