oilerscrazy
Programmer
I am stuck, I need to read text from a txt file line by line into a 2d array. I need suggestions
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
FILE *fp;
int rows=0, idx;
char f_content[10][80];
//This array will hold the contents.
//ASSUMING a line in the text file wont exceed 79 chars.
if(!(fp=fopen("TextFile.txt","r")))
{
printf("\n\tError opening file...");
exit(0);
}
while(!feof(fp) && rows<10)
fgets(f_content[rows++],80,fp);
fclose(fp);
printf("\n\tContent read from the file are...\n");
for(idx=0; idx<rows;idx++)
printf("\nROW%d:%s",idx,f_content[idx]);
printf("\n=================End of Data==================\n");
char *strings[]; // An array of strings
int iStrings = 0;
// Using fgetc()
while ( (c = fgetc(file)) != EOF) {
sprintf(strings[iStrings] + len(strings[iStrings]), c);
if (c == '\n')
iStrings++;
}
#define MAX_LINE_LENGTH 100 // Set this to something
// reasonable
char *strings[]; // An array of strings
int iStrings = 0;
// Using fgets()
while(!feof(file)) {
fgets(strings[iStrings], MAX_LINE_LENGTH, fp);
iStrings++;
}