I am trying to read a text file using input redirection ./program < file.txt, and the file I created looks like this:
Country\tSport\tGender\tMedal\n
America\tCycling\tMens\tGold\n
New Zealand\tSwimming\tWomens\tSilver\n
India\tBadminton\tMens\tbronze\n
That only reads some random data, in accordance with the first line with 4 columns, headed Country, Sport, Genderand Medal.
I also added \tand \nto make the file more readable, but in fact it has tabs and newline characters.
I am trying to read every line of this file and store them in an array of lines, which I declared as:
char *records[ROWS][COLUMNS];
I would like the array of strings to recordslook something like this:
{{"Country", "Sport", "Gender", "Medal"}, {"America", "Cycling", "Mens", "Gold"},
{"New Zealand", "Swimming", "Womens", "Silver"}, {"India", "Badminton", "Mens", "Bronze"}}
So far, I just used scanfto read lines, for example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 1000
#define COLS 30
#define MAX_CH 50
int
main(int argc, char *argv[]) {
char *records[ROWS][COLS];
char country[MAX_CH];
char sport[MAX_CH];
char gender[MAX_CH];
char medal[MAX_CH];
while (scanf("%s\t%s\t%s\t%s\n", country, sport, gender, medal) == 4) {
printf("%s %s %s %s\n", country, sport, gender, medal);
}
return 0;
}
, , New Zealand , scanf wil . scanf , 4 .
getchar() ? , getchar .