Tại sao mình sài fscanf đọc rồi in ra nhưng thừa vài kí tự như hình

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int dem(char s[])
{
	int a = 0 ;
	if(s == NULL)   return 0 ;
	else {
	   for(int i = 0 ; i < strlen(s) ; i++) {
		  if(s[i] == ' ')  a++ ;
	   }
	   return a+1 ;
    }
}

void docfile(char tenfile[],char s[])
{
	FILE *f = fopen(tenfile,"r") ;
	if(f != NULL){
		int a = 0 ;
		while(feof(f) == 0) {
			if(fscanf(f,"%c",&s[a]) == 1) {
				a++ ;
			}
		}
		fclose(f) ;
	}
	else printf("Loi File") ;
}

void xuatmang(char s[])
{
	for(int i = 0 ; i < strlen(s) ; i++) {
		printf("%c",s[i]) ;
	}
}

int main()
{
	char s[100] ;
	docfile("text.txt",s) ;
    printf("\n") ;
    xuatmang(s) ;
}

Thêm s[a]=NULL; sau khi kết thúc while.

được rồi , quên mất kí tự null . Cảm ơn bạn

NUL bạn, vì NULL là con trỏ :slight_smile:

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?