PDA

View Full Version : سوال: خوندن رشته از تويه يك فايل متني



sadegh-hut
یک شنبه 08 فروردین 1389, 02:19 صبح
مثلا يه فايل متني داريم كه به صورت زيره!!!! :

123
555
233
658
686

حالا مي خوايم طوري اين كاراكتر ها رو با كمك رشته بگيريم كه كاراكتر هاي خط بعدي توي رشته بعدي قرار بگيره .

string[0]= 123

string[1]= 555
.
.
.

ممنون اگه به سوال سادم ساده جواب بدين:لبخند:

amin1softco
یک شنبه 08 فروردین 1389, 09:10 صبح
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

int main ( void )
{
FILE *file = fopen ( "data.txt", "r" );
int i, j;
char arra[128][128];
char line[128]; /* or other suitable maximum line size */


for(i=0; i<128; i++)
for(j=0; j<128; j++)
arra[i][j] = '\0';

for(i=0; i<128; i++)
line[i] = '\0';

if ( file != NULL )
{

i=0;

while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{

strcpy(arra[i], line);
printf("array ----> %s ", &arra[i]);
i++;

}
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}


return 0;
}