[ create a new paste ] login | about

Link: http://codepad.org/Wm4yz1lO    [ raw code | output | fork | 1 comment ]

C, pasted on Dec 19:
#include<stdio.h>
#include<conio.h>
#define MAXLENGTH 128

long countWords(FILE* inFile)
{
	long count = 0;
	char word[MAXLENGTH];
	while(!feof(inFile))
	{
		int nRec = fscanf(inFile, "%s", word);
		if(nRec > 0)
		{
			count++;
		}
		else
		{
			break;
		}
	}
	return count;
}

long countWords2(FILE* inFile)
{
	long count = 0;
	char word[MAXLENGTH];
	while(fscanf(inFile, "%s", word) > 0)
	{
		count++;
	}
	return count;
}
void main()
{
	FILE* fp = fopen("Data.txt", "rt");
	if(fp != NULL)
	{
		long cnt = countWords(fp);
		printf("\nSo tu trong van ban = %d\n", cnt);
	}

	getch();
}


Output:
1
2
3
Line 17: error: conio.h: No such file or directory
In function 'main':
Line 35: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments:
posted by mouto9k on Feb 14
hàm fscanf(...) đọc đến khi gặp khoảng trắng thì dừng
+ thành công trả về số kí tự đọc được, thất bại trả về NULL
reply