Question:
I need to know how do I read several lines of text from a file in C and put each in a string.?
purpleheaded_prison_fish
2009-03-30 14:16:58 UTC
I need some help using C. Not C++, not C#, C. I need to know how do I read several lines of text from a file in C. I need to be able to put each line in a string from a file, say fopen. Please help.
Three answers:
myak
2009-03-30 14:35:43 UTC
First basic problem with strings in C is memory management - you need to know the length of the string, or line in your case, before reading it. You can of course create an array of chars that may store 100000 characters but it's a huge waste of memory because lines aren't usually that long. And there's a problem if a line is longer.



But let's assume that you don't have lines longer than 100 characters in your text file and you don't want to mess with pointers, allocating the memory and everything. Basic example of a program that reads 10 lines from a file named text.txt looks like that:



#include



int main()

{

FILE * f;

int i;

char mystring[10][100];



f = fopen ("text.txt" , "r");

// see if you can open the file, print an error otherwise

if (f == NULL) perror ("Error opening file");

else {

for (i=0;i<10;++i)

{

// if end of file is found, stop reading and get out of loop

if (fgets (mystring[i], 100 , f) == NULL) break;



puts (mystring[i]);

}

fclose (f);

}

return 0;

}



This will print 10 lines from the beginning of the file, if each of them is less than 100 characters. Notice that each line on the screen will be followed by an empty line. This is because mystring[i] already has a newline character inside it (it was read with the entire line) and puts function adds a newline to the output. So, if your file is: "hello\nhi\n" (\n is newline character), you will get an output like that:

hello



hi



The definition of fgets function is

char * fgets ( char * str, int num, FILE * stream );



What it does is reads a string from file "stream" into pointer "str" until

a) new line is found

b) end of file is found

c) the length has reached "num" characters

Whichever comes first. So if you have a first line with 200 characters, the program will read the first half and store it in mystring[0] then read another 100 into mystring[1] even though it's a single line because num = 100.



There are some tricks to avoid that but I leave that to you to figure it out :) Hope this helps!
?
2016-10-16 11:37:41 UTC
Please reproduction and paste the code below in a text textile editor, that's totally formated. My code works superbly, yet I replaced some identifier names. The text textile document I used includes the greater areas after each call. You have been on the main appropriate song yet I streamlined the code and better the loops. stable success. #contain #contain #define COL_A 20 #define COL_B 6 #define information 50 void getData(document *scoreCard, char names[information][COL_A], flow rankings[information][COL_B], int *i); int important(void) { char names[information][COL_A]; flow rankings[information][COL_B]; int i = 0, j, ok; document *scoreCard; if((scoreCard = fopen("DIV1.txt", "r")) == NULL) { printf("nNo such document."); go out(a million); } getData(scoreCard, names, rankings, &i); scoreCard = NULL; for (j = 0; j < i; j++) { for (ok = 0; ok < COL_A; ok++) { printf("%c", names[j][ok]); } for (ok = 0; ok < COL_B; ok++) { printf(" %f", rankings[j][ok]); } printf("n"); } return 0; } void getData(document *scoreCard, char names[information][COL_A], flow rankings[information][COL_B], int *i) { int j; jointly as (*i < information) { j = 0; jointly as(j < COL_A) { fscanf(scoreCard, "%c", &names[*i][j]; j++; } j = 0; jointly as (j <= COL_B) { if(fscanf(scoreCard, "%f", &rankings[*i][j]) != EOF) { j++; } else { return; } } (*i)++; } }
anonymous
2009-03-30 14:31:54 UTC
int restore(struct x* *pirm,struct x* *pask) //sumeta viska is Disko DB i RAM DB

{

char buff[BUFFLEN];

struct x *tmp;



FILE *f;

f = fopen("SDB.txt","r");

DelAll(pirm,pask);



while(fscanf(f,"%[^\n]%*c",buff) != EOF)

{



if(strncmp(buff,"------",6) == 0)

{

if(*pirm == NULL)

{

tmp = malloc(sizeof(struct x));



fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->login,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->passwd,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->vardas,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->pavarde,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->automarke,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->skundas,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->pronuomone,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->kaina,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->data,buff);



tmp->kaire = NULL;

tmp->desine = NULL;



*pirm = tmp;

*pask = tmp;



}

else

{

tmp = malloc(sizeof(struct x));



fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->login,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->passwd,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->vardas,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->pavarde,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->automarke,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->skundas,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->pronuomone,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->kaina,buff);

fscanf(f,"%[^\n]%*c",buff);

strcpy(tmp->data,buff);



tmp->kaire = *pask;

tmp->desine = NULL;



(*pask)->desine = tmp;



*pask = tmp;

}

}

}

fclose(f);

}



Ok this is a part from my C programm, little bit messy but don't mind any line that start with *, these are pointers...



fscanf(f,"%[^\n]%*c",buff) - copies a line from file "f" file , "%[^\n]%*c" - indicates to read a line until a new line character, buff is where the read line is put, this char array is of 1024 size :)



The pirm and pask, are first and last, these are pointer to a custom list :)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...