C++。
第一个问题:提示我未找到源.cpp,然后就一直是错的,调试不了。
第二个问题:怎么去把word输出到file?
#include
#include
#include
using namespace std;
#define MAX 500
#define MAX_I 5000
int read(char* file, char word[][20])
{
int k = 0, g = 0, j = 0;
//逐渐遍历file字符串,将单词保存到word数组中
for (int i = 0; i < strlen(file); i++)//strlen测量文件长度
{//碰到符号和空格
word[k][g++] = file[i];
if (file[i] == ',' || file[i] == '.' )
{
k++;
g = 0;
}
else if (file[i] == '.')
{
j++;
g = 0;
}
else if (file[i] == ' ')
{
k++;
j++;
g = 0;
}
}
return j + 1;//返回单词个数
}
void handle(char word[][20], char* p1, char* p2, int n)
{
for (int i = 0; i < n; i++)
{
if (strcmp(word[i], p1) == 0)//strcmp比较函数,两个字符相同则返回0
{
strcpy(word[i], p2);
}
}
}
void write_file(char* file, char word[][20], int n)
{
int k = 0, g = 0, j = 0;
//逐渐遍历file字符串,将单词保存到word数组中
for (int i = 0; i < strlen(file); i++)//strlen测量文件长度
{//碰到符号和空格
}
}
int main() {
char word[MAX][20] = { 0 };
char file[MAX_I] = { 0 };
char* b_rep = new char[20];
char* t_rep = new char[20];
int n;
ifstream ifs("words.txt", ios::in);
if (!ifs.is_open())//判断文件是否打开
{
cout << "文件打开失败,请保证打开文件存在" << endl;
return 0;
}
int i = 0;
//读取数据
while (!ifs.eof()) //是否读到结尾
{
file[i] = ifs.get();
i++;
}
ifs.close();
//n记录文件中单词的个数
n = read(file, word);
cout << "输入被替换单词"; cin >> b_rep;//输入被替换单词
cout << "输入替换单词"; cin >> t_rep;//输入替换单词
handle(word, b_rep, t_rep, n);
write_file(file, word, n);
ofstream ofs("new_words.txt", ios::out|ios::binary);
//写入数据
ofs.write((const char*)&file, sizeof(file));
ifs.close();
cout << "写入完成";
system("pause");
return 0;
}
#define MAX 500
#define MAX_I 5000
int read(char *file, char word[][20])
{
int k = 0, g = 0, j = 0;
//逐渐遍历file字符串,将单词保存到word数组中
// for (int i = 0; i < len; i++) // strlen测量文件长度
while (*file)
{
//碰到符号和空格
// if (file[i] == ',' || file[i] == '.')
if (*file == ',' || *file == '.' || *file == ' ')
{
k++;
g = 0;
word[k][g] = *file; //所有符号也作为单词存储。
k++;
}
else
word[k][g++] = *file;
file++;
}
// return j + 1; //返回单词个数
return k;
}
void handle(char word[][20], char *p1, char *p2, int n)
{
for (int i = 0; i < n; i++)
{
if (strcmp(word[i], p1) == 0) // strcmp比较函数,两个字符相同则返回0
{
strcpy(word[i], p2);
}
}
}
void write_file(char *file, char word[][20], int n)
{
int k = 0, g = 0, j = 0;
int len;
//逐渐遍历file字符串,将单词保存到word数组中
// for (int i = 0; i < strlen(file); i++) // strlen测量文件长度
for (int i = 0; i < n; i++) // strlen测量文件长度
{
//碰到符号和空格
strcpy(file, word[i]);
file += strlen(word[i]);
}
*file = '\0';
}
int main()
{
char word[MAX][20] = {0};
char file[MAX_I] = {0};
char *b_rep = new char[20];
char *t_rep = new char[20];
int n;
ifstream ifs("words.txt", ios::in);
if (!ifs.is_open()) //判断文件是否打开
{
cout << "文件打开失败,请保证打开文件存在" << endl;
system("pause");
return 0;
}
int i = 0;
//读取数据
while (!ifs.eof()) //是否读到结尾
{
file[i] = ifs.get();
i++;
}
file[--i] = '\0'; ///
ifs.close();
// n记录文件中单词的个数
n = read(file, word);
cout << "输入被替换单词";
cin >> b_rep; //输入被替换单词
cout << "输入替换单词";
cin >> t_rep; //输入替换单词
handle(word, b_rep, t_rep, n);
write_file(file, word, n);
ofstream ofs("new_words.txt", ios::out | ios::binary);
//写入数据
ofs.write(file, strlen(file));
// ofs.write((const char *)&file, sizeof(file));
ifs.close();
cout << "写入完成";
system("pause");
return 0;
}