#include <stdio.h>
#include <string.h>
#define FILENAME "/Users/ccj/Desktop/stu.txt"
void test()
{
FILE *fp=fopen("/Users/ccj/Desktop/stu.txt", "w");
fputs("111;xx1;xx1@qq.com\n", fp);
fputs("112;xx2;xx2@qq.com\n", fp);
fputs("113;xx3;xx3@qq.com\n", fp);
fclose(fp);
}
/*
1.将信息读取到内存
2.去掉\n
3.分割字符串
4.交换字符串
5.写文件
*/
void mySplit(char *str,char ch,char *pre,char *next)
{
while (*str!=ch)
{
*pre=*str;
str++;
pre++;
}
*pre='\0';
str++;//移到;后面
while(*str!='\0')
{
*next=*str;
next++;
str++;
}
*next='\0';
}
void swapString(char *str)
{
char others[20];
char string1[20],string2[20],string3[20];
mySplit(str, ';', string1, others);
mySplit(others, ';', string2, string3);
strcpy(str, string2);
strcat(str, ";");
strcat(str, string3);
strcat(str, ";");
strcat(str, string1);
strcat(str, "\n");
// printf("拼接好的str=%s\n",str);
}
void removeEnter(char text[][100],int index)
{
int i=0;
for (; i<index; i++)
{
long last=strlen(text[i]);
if(text[i][last-1]=='\n')
{
text[i][last-1]='\0';
}
}
}
int getText(char text[20][100])
{
int index=0;
char str[100];
FILE *fp=fopen(FILENAME, "r");
while (fgets(str, 100, fp))
{
strcpy(text[index], str);
index++;
}
fclose(fp);
return index;
}
void writeText(char text[][100],int index)
{
FILE *fp=fopen(FILENAME, "w");
int i=0;
for (; i<index; i++)
{
fputs(text[i], fp);
}
fclose(fp);
}
int main()
{
char text[20][100];
int index=getText(text);
removeEnter(text,index);
int i=0;
for (i=0; i<index; i++)
{
swapString(text[i]);
}
writeText(text,index);
return 0;
}
网友评论