c语言中把文件中的空格去除
发布网友
发布时间:2023-01-07 12:51
我来回答
共4个回答
热心网友
时间:2023-10-20 05:49
第一种:使用位域*读取的长度;
第二种:可以直接按照结构体来读写;
实例代码如下:
#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
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!\n");
exit(-1);
}
printf("Please input four times Roommate data: Name NO Addr\n");
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);
/* Read from file*/
file = NULL;
if(!(file = fopen("a.txt", "r"))) {
printf("Create File failed!\n");
exit(-1);
}
printf("Read from the file: Name NO Addr\n");
fread((void *)Rom, sizeof(struct Roommate), 2, file);
for(int i=0; i<2; ++i) {
printf("i=%d Name:%s\tNO:%s\tAddr:%s\n", i, Rom[i].name, Rom[i].NO, Rom[i].addr);
}
fclose(file);
while(getchar()) ;
return 0;
}
热心网友
时间:2023-10-20 05:49
你的这个 f 是文件指针,不是数据,使用 fgets(buffer, len, f); buffer为字符串也可以,然后就可以使用你的buffer++了 。
热心网友
时间:2023-10-20 05:50
#include <stdio.h>
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 ;
{
characBuf[100+1];
characBufTmp[100+1];
int i,j=0;
for( i=0 ; i<=strlen(pcBufTmp)-1 ; i++ )
{
if( pcBufTmp[i] != ' ' )
{
pcBuf[j] = pcBufTmp[i] ;
j++ ;
}
}
}
热心网友
时间:2023-10-20 05:50
#include<stdio.h>
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;
}