How to read a file into an array of structs? - c

Im trying to read a text file into an array of structs, but when trying to print the array, the struct is empty. The printing function works fine and I think the problem is in getRawData.
struct student
{
char ID[MAXID + 1];
char f_name[FIRST_NAME_LENGTH + 1];
char s_name[LAST_NAME_LENGTH + 1];
int points[MAXROUNDS];
};
//main//
case 'W':
if(save(array, len) == 0);
{
printf("Data saved.\n");
}
break;
case 'O':
if(getRawData(array, len));
{
printf("File read.\n");
}
break;
int save(struct student *h, int num_students)
{
char name[20];
printf("Enter file name: " );
scanf("%s", name); // Read in filename
FILE *output = fopen(name, "w"); // open the file to write
if (!output) {
return -1; // error
}
for (int i = 0; i < num_students; ++i)
{
fprintf(output, "%s %s %s \n", h[i].f_name, h[i].s_name, h[i].ID);
for(int j = 0; j < MAXROUNDS; j++)
{
fprintf(output, "%d\n", h[i].points[j]);
}
printf("Information of student %s %s (%s) written into file %s\n", h[i].s_name, h[i].f_name, h[i].ID, name);
}
fclose(output); // close
return 0;
}
int getRawData(struct student *records)
{
int i;
int nmemb; // amount of structs
char name[20];
printf("Name of the file to be opened: \n");
scanf("%s", name);
FILE *outtput = fopen(name, "r");
int ch=0;
int lines=0;
if (outtput == NULL);
return 0;
lines++;
while(!feof(outtput))
{
ch = fgetc(outtput);
if(ch == '\n')
{
lines++;
}
}
nmemb = lines / 7;
for(i = 0; i < nmemb; i++) {
fscanf(outtput, "%s %s %s", records[i].f_name, records[i].s_name, records[i].ID);
for(int j = 0; j < MAXROUNDS; j++)
{
fscanf(outtput, "%d\n", &records[i].points[j]);
}
}
printf("%d", lines);
return i;
}
So my goal is to get the data from the file and write it over whatever there is stored in the struct array. I would appreciate some help as I have been working on this for way too long.

Look at this code in getRawData(), first you are reading file to identify total number of lines:
while(!feof(outtput))
{
ch = fgetc(outtput);
if(ch == '\n')
.....
.....
due to this the file stream pointer pointing to EOF and after this, in the for loop, you are doing:
for(i = 0; i < nmemb; i++) {
fscanf(outtput, "%s %s %s", records[i].f_name, records[i].s_name, records[i].ID);
.....
.....
Here, the fscanf() must be returning the EOF because there is nothing remain to read from stream file. You should check the return value of fscanf() while reading file.
You should reset the pointer to start of file before reading it again. You can use either rewind(ptr) or fseek(fptr, 0, SEEK_SET). Below is a sample program to show you what is happening in your code and how the solution works:
#include <stdio.h>
int main (void) {
int ch;
int lines = 0;
char str[100];
FILE *fptr = fopen ("file.txt", "r");
if (fptr == NULL) {
fprintf (stderr, "Failed to open file");
return -1;
}
while (!feof(fptr)) {
ch = fgetc (fptr);
if(ch == '\n') {
lines++;
}
}
printf ("Number of lines in file: %d\n", lines);
printf ("ch : %d\n", ch);
printf ("Now try to read file using fscanf()\n");
ch = fscanf (fptr, "%s", str);
printf ("fscanf() return value, ch : %d\n", ch);
printf ("Resetting the file pointer to the start of file\n");
rewind (fptr); // This will reset the pointer to the start of file
printf ("Reading file..\n");
while ((ch = fscanf (fptr, "%s", str)) == 1) {
printf ("%s", str);
}
printf ("\nch : %d\n", ch);
fclose (fptr);
return 0;
}
The content of file reading in the above program:
Hello Vilho..
How are you!
Output:
Number of lines in file: 2
ch : -1
Now try to read file using fscanf()
fscanf() return value, ch : -1
Resetting the file pointer to the start of file
Reading file..
HelloVilho..Howareyou!
ch : -1
Here you can see, the first ch : -1 indicate that the file pointer is at EOF and if you try to read you will get EOF because there is nothing left to read. After resetting file pointer, you can see fscanf() is able to read file.
You should not use while (!feof(file)). Check this.

Related

C Input value from .txt to struct

I need to scan values from .txt to a structure so I can work further on with my program. I've been trying various methods from the thread and this is the closes I got to a successful build.
I can NOT get the values to be printed out so I can test if they scanned correctly before working my way further into the program.
I have different values in struct:
struct knyga
{
char vardas[10];
char pavadinimas[50];
int metai;
double kaina;
};
and this is my reading function:
void Skaitymas(struct knyga arr_knyga[]){
FILE *fp;
fp = fopen("duomenys.txt", "r");
int skaicius;
int txt;
txt = fgetc(fp);
while((txt = fgetc(fp)) != EOF){
if(txt == '\n') skaicius++;
txt = fgetc(fp);
}
printf("%d", skaicius);
fp = fopen("duomenys.txt", "r");
for(int i = 0; i < skaicius; i++){
fscanf(fp, "%s %s %d %lf", arr_knyga[i].vardas, arr_knyga[i].pavadinimas, &arr_knyga[i].metai, &arr_knyga[i].kaina);
}
fclose(fp);
}
EDIT:
This is the content of my text file:
Onute Knyga 1999 12.12
Petras Knygute 2001 9.99
EDIT 2:
my main function:
int main() {
struct knyga arr_knyga[10];
Skaitymas(arr_knyga);
return 0;
}
You call txt = fgetc(fp); too often. The two occurrences of this line must be removed.
Especially in the loop you have one call to fgetc that is checked for '\n' and a second call that is not checked, so there is a 50%/50% chance that a '\n' is not counted.
You forgot to initialize the counter variable.
The counting would be correct with this version:
void Skaitymas(struct knyga arr_knyga[]){
FILE *fp;
fp = fopen("duomenys.txt", "r");
int skaicius = 0;
int txt;
while((txt = fgetc(fp)) != EOF){
if(txt == '\n') skaicius++;
}
printf("%d", skaicius);
fclose(fp);
}
But it would be better to omit the line-counting and detect the end-of-file condition in the fscanf loop.
void Skaitymas(struct knyga arr_knyga[]){
FILE *fp;
fp = fopen("duomenys.txt", "r");
int skaicius = 0;
int rc;
while(1)
{
rc = fscanf(fp, "%s %s %d %lf", arr_knyga[skaicius].vardas, arr_knyga[skaicius].pavadinimas, &arr_knyga[skaicius].metai, &arr_knyga[skaicius].kaina);
if(rc == 4)
{
skaicius++;
}
else
{
break;
}
}
if(!feof(fp))
{
fprintf(stderr, "error reading file or wrong data after line %d\n", skaicius);
}
else
{
printf("%d", skaicius);
}
fclose(fp);
}

I want to concatenate n files but the concatenation is not in correct order?

I am trying to write a C program to concatenate N files which I should read them from the keyboard and then take all of their concent starting from FILE1 to n and put them on a NEWFILE, my program concatinates all of the N files but the order is not from 1 to n. For example:
I input N=3 and I put three files:
text1.txt (inside the file I have "We cannot")
text2.txt ("live")
text3.txt ("without water.")
and then save the concatination on a finalresult.txt
Now my finalresult.txt should be:
We cannot
live
without water.
But my result is:
withoutwater. We cannot live
Here is my full program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int number;
scanf("%d", &number);
int i;
char a[number][50];
char help[50];
for(i=0; i<number; i++)
{
scanf("%s", help);
strcpy(a[i], help);
}
printf("Concating the content of %d files ...\n", number);
FILE * filer, * filew;
int numr,numw;
char buffer[64];
for(i=0; i<number; i++)
{
filer = fopen(a[i], "rt");
if(filer == NULL)
{
fprintf(stderr, "open read file error.\n");
exit(1);
}
filew = fopen("finalresult.txt","a+t");
if(filew==NULL) {
fprintf(stderr,"open write file error.\n");
exit(1);
}
while(feof(filer)==0) {
numr=fread(buffer,1,100,filer);
if(numr!=100) {
if(ferror(filer)) {
fprintf(stderr,"read file error.\n");
exit(1);
}
}
numw=fwrite(buffer,1,numr,filew);
if(numw!=numr) {
fprintf(stderr,"write file error.\n");
exit(1);
}
}
}
fclose(filer);
fclose(filew);
return 0;
}
You can rewrite this
char a[number][50];
char help[50];
for(i=0; i<number; i++)
{
scanf("%s", help);
strcpy(a[i], help);
}
as
char a[number][50];
for(i=0; i < number; ++i)
scanf("%s", a[i]);
you don't need another intermediate buffer. Also bear in mind, that "%s"
matches only non-empty characters, if your filename has an empty character,
scanf won't read the whole input and leave behind extra characters in the input
buffer, thus messing with the next scanf call. Here it would be better to use
fgets.
char a[number][50];
for(i = 0; i < number; ++i)
{
if(fgets(a[i], sizeof a[i], stdin) == NULL)
{
fprintf(stderr, "Could not read the filename\n");
exit(1);
}
a[i][strcspn(a[i], "\n")] = 0; // removing newline
}
I haven't seen the mode 't' for fopen, is that a an extension of the modes
for Windows?
A problem with your code is that you are calling
filew = fopen("finalresult.txt","a+t");
inside the loop but you never close it inside the loop. FILE* file is buffered,
that means when you use fprintf(file,...) or fwrite(..., file)
the content gets buffered and the content is physically written in the file at a
later point, for example when the buffer is full or you use fflush(filew). So
when you fopen the same file before doing a fflush or fclose, the old
buffered content remains in the buffer and is written at a much later point (in
your case when the program exits), thus overwriting your new content. Your new content
will have the same fate. At the end you end up with a mess because the content
gets overwritten at the end of the program. That's why you see "garbage" in the
file.
So, you have to do fclose before doing fopen with the same filename, or
better you should do the fopen before the loop.
Also this check is incorrect:
if(numr!=100) {
if(ferror(filer)) {
fprintf(stderr,"read file error.\n");
exit(1);
}
}
This would only be correct, if the file size is a multiple of 100. If it's not,
the last block will have less than 100 bytes and you would end your program,
even though the fread had no errors.
So I'd rewrite your program like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(void)
{
size_t num_of_files;
printf("Enter the number of files: ");
fflush(stdout);
if(scanf("%zu", &num_of_files) != 1)
{
fprintf(stderr, "Could not read the number of files\n");
exit(1);
}
int c;
// clearing the input buffer
while((c = getchar()) != '\n' && c != EOF);
char fnames[num_of_files][50];
for(size_t i = 0; i < num_of_files; ++i)
{
printf("Enter the filename %zu: ", i+1);
fflush(stdout);
if(fgets(fnames[i], sizeof fnames[i], stdin) == NULL)
{
fprintf(stderr, "Could not read the filename\n");
exit(1);
}
fnames[i][strcspn(fnames[i], "\n")] = 0; // removing newline
}
FILE *filew = fopen("finalresult.txt", "wt");
if(filew == NULL)
{
fprintf(stderr, "Could not open file finalresult.txt for writing: %s\n",
strerror(errno));
exit(1);
}
for(size_t i = 0; i < num_of_files; ++i)
{
FILE *filer = fopen(fnames[i], "rt");
if(filer == NULL)
{
fprintf(stderr, "could not open %s for reading, skipping: %s\n",
fnames[i], strerror(errno));
continue;
}
char buffer[100];
size_t len;
while((len = fread(buffer, 1, sizeof buffer, filer)) != 0)
{
if(fwrite(buffer, 1, len, filew) != len)
{
fprintf(stderr, "Error writing finalresult.txt\n");
fclose(filer);
fclose(filew);
exit(1);
}
}
if(!feof(filer))
fprintf(stderr, "file %s could not be read completely\n", fnames[i]);
fclose(filer);
}
fclose(filew);
return 0;
}

Line counting in C but exclude empty lines

I've got the following program, but there is a problem. First the program part that does not work:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
FILE *fp2;
char ch;
char fnamer[100];
char fnamer2[100]; //Storing File Path/Name of Image to Display
printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n");
scanf("%s",&fnamer);
fp=fopen(fnamer,"r");
if(fp==NULL)
{
printf("Error!");
exit(1);
}
printf("\n\nPlease Enter the Full Path of the Image file you want to write to: \n");
scanf("%s",&fnamer2);
fp2=fopen(fnamer2,"w");
if(fp2==NULL)
{
printf("Error!");
exit(1);
}
else
{
// printf("test\n");
}
int line_number = 0;
int charsOnLine = 0;
fprintf(fp2, "%d: ", ++line_number); /* put line number in output file */
printf("%d: ", line_number);
while((ch = fgetc(fp)) != EOF )
{
//printf("test2");
fputc(ch,fp2);
printf("%c", ch);
if ( ch != '\n' && charsOnLine ==0 )
{
fprintf(fp2, "%d:", ++line_number ); /* put line number in output file */
printf("%d: ", line_number);
}
// if (ch != '\n' && charsOnLine 0 ){
// fprintf(fp2, "%c", ch);
// printf("%d", ch);
// }
}
fclose;
fclose(fp);
// fclose(fp2);
// getch();
}
The program needs to count the lines, give them a number but skip the blank lines. But here is the problem: when I run this code it gives all the chars a number.
You could use a flag to remember when you have just started a new line. The first time you see a char not equal to '\n', you print the line number and clear the flag.
Something like:
int line_number = 0;
int newLine = 1;
while((ch = fgetc(fp)) != EOF )
{
if (ch == '\n')
{
newLine = 1;
}
else
{
if (newLine)
{
fprintf(fp2, "%d:", ++line_number );
printf("%d: ", line_number);
newLine = 0;
}
}
fputc(ch,fp2);
printf("%c", ch);
}
From what I understand, the program needs to count the lines and append their content.
Then I would search for '\n' char, rather than skipping it with if (ch != '\n')
int main()
{
FILE *fp;
FILE *fp2;
char ch;
char fnamer[100];
char fnamer2[100]; //Storing File Path/Name of Image to Display
printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n");
scanf("%s",&fnamer);
fp=fopen(fnamer,"r");
if(fp==NULL)
{
printf("Error!");
exit(1);
}
printf("\n\nPlease Enter the Full Path of the Image file you want to write to: \n");
scanf("%s",&fnamer2);
fp2=fopen(fnamer2,"w");
if(fp2==NULL)
{
printf("Error!");
exit(1);
}
int line_number = 0;
fprintf(fp2, "%d: ", ++line_number );
while((ch = fgetc(fp)) != EOF )
{
if ( ch == '\n' )
{
fprintf(fp2, "%d: ", ++line_number ); /* put line number in output file */
}
else {
fputc(ch,fp2); /* put the char in the corresponding line */
}
}
fclose(fp);
fclose(fp2);
}

How to code my own version of tail unix command in C language?

I want to write my own code for tail Unix command but I am having a lot of trouble doing that. I am completely new to C language and apparently lost on how to fix my code. I am having number of problems regarding my code:
I am unable to read and print lines from text file in the if statements it is not printing any string from file when I run it don't know why?
Unable to print specific lines in if statement by taking user input as starting line and then printing till the End of File.
I am having trouble figuring out the right solution to my problems and debugging what problems there are in code.
I would really appreciate your help in figuring how to do all the above in my code. If someone can help make changes and get my code to work right.
#include <stdio.h>// for fopen, fscanf, fclose, fprintf
#include <stdlib.h>// for exit
#include <string.h>
int main(int argc, const char * argv[]){
printf("Opening file\n");
char filename[64]; // file attribute
strcpy(filename, argv[1]); //copy string from argv[1] to filename
printf("FILENAME: %s \n", filename);
FILE* fp; // file pointer
int ch, linestotal = 0, user;
char c[10000];
if(argv[2]){ //checking input argv[2]
user = atoi(argv[2]); // char to int
}
fp = fopen( filename, "r"); // file read
if(fp == NULL){ // verify file is opened
printf("Error opening file");
exit(1);
}
while(!feof(fp)) // check end of file
{
ch = fgetc(fp);
if(ch == '\n')
{
linestotal++; //Checking total lines inside file
}
}
printf("Total no. of lines: %d\n", linestotal );
printf("User input: %d\n", user );
printf("**********************\n");
if (!user && linestotal<= 10)
{
while ( (ch = fgetc(fp) ) != EOF)
printf("%c", ch);
fclose(fp);
printf("********************\n");
}if(!user && linestotal>10) { // to print 10 lines
for(int i = (linestotal-10); i <= (linestotal); i++)
{ c[i] = fgetc(fp);
printf("%c", c[i]);
}
fclose(fp);
printf("********************\n");
}if(user && user<linestotal) {
for(int i = (linestotal-user); i <= (linestotal); i++)
{ c[i] = fgetc(fp);
printf("%c", c[i]);
}
fclose(fp);
printf("********************\n");
}if(user && user>linestotal){
while ( (ch = fgetc(fp) ) != EOF)
printf("%c", ch);
fclose(fp);
printf("********************\n");
}else{
printf("Unable to read and print file \n");
}
printf("End of file");
return 0;
}

Reading from two .txt file using fscanf in C, storing in structs and outputitng in .txt file

Description: program read data from 2 files, store them in structs,ask user for (city or place of residence), if city name matches with that stored in file, program displays output(student, national_ID,name) and store in a file.
My question is that, the above code that i wrote does not work. it gives me a "no information" even when i enter a city which is on the file.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 20
#define MAX_LINE 7
typedef struct studentdata
{
char NATIONAL_ID[20];
char NAME[20];
char STUDENT_CODE[20];
char CITY[20];
}studentdata;
int main(void)
{
int i;
char x=0, CITY[MAX_LEN];
studentdata y[MAX_LINE];
char temp[20];
char temp1[20];
char count=0;
FILE *fi = fopen("employee1.txt", "r");
if (fi == NULL)
{
printf("error data");
exit(0);
}
FILE *fp = fopen("student1.txt", "r");
if (fp == NULL)
{
printf("error data1");
exit(1);
}
i = 0;
printf("Enter city\n");
scanf("%s",CITY);
//i = 0;
FILE *fa = fopen("student2.txt", "w");
if (fa == NULL)
{
printf("error data2");
exit(2);
}
while(fscanf(fi, "%s %s %s", y[i].NATIONAL_ID, y[i].NAME, y[i].STUDENT_CODE) == 4)
i++;
count=i;
I am sure the error is within this loop but can't just find it.
while(fscanf(fp, "%s %s", temp,temp1) == 2)
{
for(i=0; i< count;i++)
{
if (strcmp(y[i].NATIONAL_ID,temp)==0)
{
strcpy(y[i].CITY,temp1);
if (strcmp(y[i].CITY,CITY)==0)
{
fprintf( "%s\t %s\t %s\t %s\t\n", y[i].NATIONAL_ID, y[i].NAME, y[i].STUDENT_CODE, y[i].CITY);
x++;
}
}
}
}
fclose(fa);
if(!x)
{
printf("no information\n");
}
fclose(fi);
fclose(fp);
return 0;
}
I would say that nothing is being read from file into the in first while loop -
while(fscanf(fi, "%s %s %s", y[i].NATIONAL_ID, y[i].NAME, y[i].STUDENT_CODE) == 4)
i++;
As you match for 3 arguments but checks fscanf's return against 4 which will be false and loop will not iterate and i remains 0 , so as count.
Therefore , your this inner loop won't run-
for(i=0; i< count;i++) //count=0
and thus you don't get your output .
Modify your loop to -
while (fscanf(fi, "%s %s %s", y[i].NATIONAL_ID, y[i].NAME, y[i].STUDENT_CODE) ==3)
/* see fscanf's return is checked against 3 */
i++;
Note that an easy way to spot this problem would be to print the information as it is read, or print the array after the read is complete.

Resources