hi

i am trying to copy the returned value (not really the returnd pointer but what it points at) from function gets but its not working

and when i try to print it or print the str_infile before strcpy function , its not printed out !!

i added temp_buff before but made alot of problems so i removed it !

i need to store the lines from C_data file into array like this

char temp_buff [400] ;

temp_buff[0] = "first line "
temp_buff[1] = "2bd line "
temp_buff[3] = "3rd line "

and so on !

i tried

for (i = 0 ; i <sizeof (file_p)-1;i++ ) 
printf ("buff_lines \n " ,& temp_buff[i]) ;

or even 

printf("buff\n " ,temp_buff);

sure in loop but when i do such thing i dont get output or i get error

why cant i do this

temp_buff = fgets(str_infile,70,infile );
or
temp_buff = str_infile ;

or even save the value returned from puts to the array ?

i need to use temp_buffer as seprate array after that

thank you :)

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


int main(){
int i =0;
char str_infile[400];
char *file_p ; // file pointer to what fgets return 
FILE *infile;
if((infile=fopen("C_data","r"))==NULL){
printf("\nUnable t open file C_data");
return -1 ;
}

while(fgets(str_infile,70,infile)!=NULL){
puts(str_infile );
printf(" \n " ,& str_infile) ; // this line doesnt work at all !
i ++;
file_p = fgets(str_infile,70,infile );

}

for (i = 0 ; i <sizeof (file_p)-1;i++ ) 
printf ("buff_lines \n " , file_p ) ;


fclose(infile);



return 0;
}

Your array to hold the file's contents needs to be 2D (rows and columns): array[rows][cols].

Each time you want to save a row, copy it to array[rows] - that serves as the array pointer for that row. (In C, 2D arrays are arrays (rows) inside the larger array.)

The general technique is:

char *ptr;
char buff[256];
char array[20][256];  //20 rows, each 256 chars long
int row=0;
//where fp is your FILE pointer and has open the file already
do {
   ptr=fgets(buff, 256, fp);
   if(ptr) {                    //you have good data, not EOF
      strcpy(myCharArray[row],buff); //move the data - it's good
      row++;                    //set up for the next row
   }
}while(ptr);

This avoids putting a duplicate last line of data, (when EOF is reached), into the array, or just having a blank row.

Why is not a great question - many functions in C work with the format or parameters, inside the ():
printf(), scanf(), toupper, tolower() sqrt(), strcpy(), strcmp(), etc. Pointers are typically returned (scanf() for instance), or a number (printf(), strcmp(), etc.).

On line 18 of your code, remove the & from the printf(). That's for scanf().

ok that was helpful .
thank you for the explanation and correction

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.