so I am trying to read a file with various university names with laptops and other information in c and then print them to the screen with a calculated cost/gb of ram, but am having trouble checking for EOF. I tried fscanf but this took away the first character of my first string, and am now trying fgets but this is taking away the first line of the file. I am wondering how I can avoid either of these while still checking for EOF. Thanks.
#include <stdio.h>
int main() {
FILE *text; //Defining file pointer
char end[100]; //EOF check
//Headers
char name[20] = "University";
char lap[20] = "Laptop";
char ram_[20] = "Ram";
char gb_[20] = "Storage";
char price_[20] = "Price";
char costram_[20] = "Cost/Gb of ram";
int ram;
int gb;
float price;
float costram; //Cost/gb of ram
printf("\n%10s %12s %8s %10s %12s %16s\n", name, lap, ram_, gb_, price_, costram_);
text = fopen("text.txt", "r");
//If file cannot be opened
if (text == NULL){
printf("Error opening file");
}
//File is opened
else{
//fscanf took away the first letter in my first string since it scanned it so I used fgets to compare with NULL instead
while (fgets(end, 100, text) != NULL){
fscanf(text, "%s%s%d%d%f", name, lap, &ram, &gb, &price);
costram = price / ram;
printf("\n%10s %12s %6dgb %8dgb %12.2f %12.2f", name, lap, ram, gb, price, costram);
}
}
return 0;
}
Replit link:https://repl.it/@Michael2022/Hardware-Assignment