c语言中把文件中的空格去除
答案:4 悬赏:80 手机版
解决时间 2021-11-23 09:41
- 提问者网友:听门外雪花风
- 2021-11-23 05:02
c语言中把文件中的空格去除
最佳答案
- 五星知识达人网友:荒野風
- 2021-11-23 05:22
第一种:使用位域限制读取的长度;
第二种:可以直接按照结构体来读写;
实例代码如下:
#include "stdafx.h"
#include
#include
#include
struct Roommate{
char name[6];
char NO[8];
char addr[10];
};
int _tmain(int argc, _TCHAR* argv[])
{
struct Roommate Rom[2] = {0};
FILE *file = NULL;
if(!(file = fopen("a.txt", "w"))) {
printf("Create File failed!
");
exit(-1);
}
printf("Please input four times Roommate data: Name NO Addr
");
for(int i=0; i<2; ++i) {
scanf("%s%s%s", Rom[0].name, Rom[0].NO, Rom[0].addr);
fwrite((const void *)&Rom[0], sizeof(struct Roommate), 1, file);
}
fclose(file);
file = NULL;
if(!(file = fopen("a.txt", "r"))) {
printf("Create File failed!
");
exit(-1);
}
printf("Read from the file: Name NO Addr
");
fread((void *)Rom, sizeof(struct Roommate), 2, file);
for(int i=0; i<2; ++i) {
printf("i=%d Name:%s NO:%s Addr:%s
", i, Rom[i].name, Rom[i].NO, Rom[i].addr);
}
fclose(file);
while(getchar()) ;
return 0;
}
第二种:可以直接按照结构体来读写;
实例代码如下:
#include "stdafx.h"
#include
#include
#include
struct Roommate{
char name[6];
char NO[8];
char addr[10];
};
int _tmain(int argc, _TCHAR* argv[])
{
struct Roommate Rom[2] = {0};
FILE *file = NULL;
if(!(file = fopen("a.txt", "w"))) {
printf("Create File failed!
");
exit(-1);
}
printf("Please input four times Roommate data: Name NO Addr
");
for(int i=0; i<2; ++i) {
scanf("%s%s%s", Rom[0].name, Rom[0].NO, Rom[0].addr);
fwrite((const void *)&Rom[0], sizeof(struct Roommate), 1, file);
}
fclose(file);
file = NULL;
if(!(file = fopen("a.txt", "r"))) {
printf("Create File failed!
");
exit(-1);
}
printf("Read from the file: Name NO Addr
");
fread((void *)Rom, sizeof(struct Roommate), 2, file);
for(int i=0; i<2; ++i) {
printf("i=%d Name:%s NO:%s Addr:%s
", i, Rom[i].name, Rom[i].NO, Rom[i].addr);
}
fclose(file);
while(getchar()) ;
return 0;
}
全部回答
- 1楼网友:慢性怪人
- 2021-11-23 07:57
我暂时保留我的看法!
- 2楼网友:酒安江南
- 2021-11-23 06:57
#include
main()
{
char acBuf[100+1];
char acBufTmp[100+1];
memset( acBuf , 0x00 , sizeof( acBuf ) );
memset( acBufTmp , 0x00 , sizeof( acBufTmp ) );
strcpy( acBufTmp , " aaa bbb ccc " ) ;
GetStr( acBufTmp , acBuf ) ;
printf( "字符串%s,字符串去掉空格的长度%d\n" , acBuf , strlen(acBuf)) ;
}
int GetStr( pcBufTmp , pcBuf )
char *pcBufTmp ;
char *pcBuf ;
{
char acBuf[100+1];
char acBufTmp[100+1];
int i,j=0;
for( i=0 ; i<=strlen(pcBufTmp)-1 ; i++ )
{
if( pcBufTmp[i] != ' ' )
{
pcBuf[j] = pcBufTmp[i] ;
j++ ;
}
}
}
main()
{
char acBuf[100+1];
char acBufTmp[100+1];
memset( acBuf , 0x00 , sizeof( acBuf ) );
memset( acBufTmp , 0x00 , sizeof( acBufTmp ) );
strcpy( acBufTmp , " aaa bbb ccc " ) ;
GetStr( acBufTmp , acBuf ) ;
printf( "字符串%s,字符串去掉空格的长度%d\n" , acBuf , strlen(acBuf)) ;
}
int GetStr( pcBufTmp , pcBuf )
char *pcBufTmp ;
char *pcBuf ;
{
char acBuf[100+1];
char acBufTmp[100+1];
int i,j=0;
for( i=0 ; i<=strlen(pcBufTmp)-1 ; i++ )
{
if( pcBufTmp[i] != ' ' )
{
pcBuf[j] = pcBufTmp[i] ;
j++ ;
}
}
}
- 3楼网友:从此江山别
- 2021-11-23 05:43
#include
int main()
{
FILE *fp;
FILE *out;
char ch;
fp=fopen("1.txt","r");
if(fp==NULL)
{
printf("cannot open file\n");
exit(1);
}
out=fopen("out.txt","w");
if(fp==NULL)
{
printf("cannot create file\n");
exit(1);
}
printf("please wait...\n");
while(!feof(fp))
{
ch=fgetc(fp);
if(ch!=' ')
{
fputc(ch,out);
}
}
printf("process complete\n");
fclose(fp);
fclose(out);
return 0;
}
int main()
{
FILE *fp;
FILE *out;
char ch;
fp=fopen("1.txt","r");
if(fp==NULL)
{
printf("cannot open file\n");
exit(1);
}
out=fopen("out.txt","w");
if(fp==NULL)
{
printf("cannot create file\n");
exit(1);
}
printf("please wait...\n");
while(!feof(fp))
{
ch=fgetc(fp);
if(ch!=' ')
{
fputc(ch,out);
}
}
printf("process complete\n");
fclose(fp);
fclose(out);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯