12-10-2005, 15:22
|
|
|
|
חבר מתאריך: 04.08.02
הודעות: 4,468
|
|
....
תיכף נכנס כיפור, אז אני אמהר.
כתבתי לך משהו בסיסי:
קוד:
#include <stdio.h>
#include <stdlib.h>
#define MAX_WORD_SIZE 256
#define MAX_WORDS 2048
#define FILE_NAME "C:\\test.txt"
int main( int argc, char *argv[] )
{
char sWords[MAX_WORDS][MAX_WORD_SIZE];
int nCh, nWord, nIndex;
FILE *fpInput;
fpInput = fopen( FILE_NAME, "r" );
if( fpInput == NULL )
{
perror( "fopen: " );
return -1;
}
nWord = 0; nIndex = 0;
while( ( nCh = fgetc( fpInput ) ) != EOF )
{
if( nCh == ' ' || nCh == '\n' )
{
sWords[nWord][nIndex] = '\0';
++nWord; nIndex = 0;
}
else
{
sWords[nWord][nIndex] = nCh;
++nIndex;
}
}
while( nWord > 0 )
printf( "%d => %s\n", nWord, sWords[--nWord] );
fclose( fpInput );
return 0;
}
תעבוד עליו, כמובן יש בעיות שם (בכוונה) שתוכל לתרגל:
1. התוכנית לא מבדילה בין תו אחד למס' תווים.
תקן את זה ע"מ שלא תקבל איבר במערך שכולל רק רווח.
2. תעבוד על דינמיות, פה הגדלים של המחרוזת קבועים. תחשוב בכיוון של יצירת מחרוזת on-the-fly, realloc, malloc וכו'.
_____________________________________
C pogrammers never die. They are just cast into void.
|