fscanf string from file - c

I have i file "student_read.txt" that my code is supposed to read from.
the file contains this:
3872187
John Doe
21
then its going to print the information as seen in the print_student function. but it seems like when it reads from the file with fscanf it detects the space between John and doe as enter which makes it so the output is.
student id: 3872187
full name: John
age: Doe
what can i do to make it print the output:
student id: 3872187
full name: john doe
age: 21
#include <stdio.h>
#include <string.h>
#define STRING_LENGTH 100
//Struct with alias student_t that contains student information.
typedef struct student_t{
char studentId[STRING_LENGTH];
char studentName[STRING_LENGTH];
char studentAge[STRING_LENGTH];
}student_t;
//function for printing the student information
void print_student(struct student_t student){
printf("\nStudent id: %s\n", student.studentId);
printf("Name: %s\n", student.studentName);
printf("Age: %s\n", student.studentAge);
}
int main() {
//Use the defined struct to crate an instance of Student
struct student_t student;
//Zero out all the memory of the struct instance
memset(&student, 0, sizeof(student));
//selecting option
int option;
printf("Choose an option");
scanf("%i", &option);
switch(option){
case 1:{
FILE* read = fopen("student_read.txt", "r");
fscanf(read, "%s", &student.studentId);
fscanf(read, "%s", &student.studentName);
fscanf(read, "%s", &student.studentAge);
print_student(student);
}
break;
case 2:{
//Asks for student_t id
printf("\nStudent id: ");
scanf("%s", &student.studentId);
//getchar(); is used to prevent newline in input of fgets function.
getchar();
//Asks for full name (strcpy since datatype = string)
char name[STRING_LENGTH] = {0};
printf("\nFull name: ");
fgets(name, STRING_LENGTH, stdin);
name[strlen(name)- 1] = 0;
strcpy(student.studentName, name);
//Asks for age
printf("\nAge: ");
scanf("%s", &student.studentAge);
}
break;
case 3:{
printf("Program closing");
}
break;
default:
printf("Invalid Option... Try again");
}
/*
FILE* write = fopen("student_write.txt", "w");
if (read==0){
printf("failed to open file\n");
return -1;
}
fclose(read);
fclose(write);
*/
return 0;
}

You can use fgets instead of fscanf. This function will be reading characters until it finds a newline character or end-of-file, so it won't stop at the whitespace.
Edit: if you need to use fscanf compulsory, you can check this response: R: Can fscanf() read whitespace?

Related

fprintf function in code writes garbage data into .csv file

I'm trying to create a C program which collect's an applicant's information. When a user is prompted to enter their written subjects, the program writes rubbish data into the .csv file when they wrote one. And sometimes does the same when the number of subjects written is two.
I've tried to clear the buffer stream, but it's no use. Strangely, using different compliers like DevC++, Embarcadero DevC and VS Code produces different results.
Edit: I've also noticed the chances of the rubbish values being written into the file are lowered when the grades of the subjects is lower than the number of subjects written.
Attached below is the code. And an image of the output.
// C libraries.
#include <stdio.h> // Contains function prototypes for the standard input/output library functions, and information used by them.
#include <conio.h> // Contains function prototypes for the console input/output library functions.
#include <stdlib.h> // Contains function prototypes for conversions of numbers to text and text to numbers, memory allocation, random numbers and other utility functions.
#include <string.h> // Contains function prototypes for string-processing functions.
#include <time.h> // Contains function prototypes and types for manipulating the time and date.
#include <stdbool.h> // Contains macros defining bool, true and false, used for boolean variables.
struct Applicant
{
int applicationID;
int dateOfApplication;
char lastName[21];
char firstName[21];
char middleName[21];
char dateOfBirth[21];
int age;
char gender;
char address[100];
char phoneNumber[21];
char emailAddress[51];
char mobileNumber[21];
int numSubjectsWritten;
char csecSubjects[20][100];
char grades[20];
char programmeSelection[10];
};
struct Applicant getApplicantData()
{
struct Applicant applicant;
int i = 0;
int numSubjects;
// Asking for applicant input for various fields.
printf("| Personal |");
printf("\nEnter Last Name: ");
scanf(" %20s", &applicant.lastName);
fflush(stdin);
printf("\nEnter First Name: ");
scanf(" %20s", &applicant.firstName);
fflush(stdin);
printf("\nEnter Middle Name (If you don't have a middle name, leave this field blank.): ");
gets(applicant.middleName);
fflush(stdin);
/*
printf("\nEnter Date of Birth: ");
scanf(" %s", &applicant.dateOfBirth);
fflush(stdin);
printf("\nEnter Gender. 'M' for male, 'F' for female, (M|F): ");
scanf(" %c", &applicant.gender);
fflush(stdin);
printf("\n\n| Contact Information |");
printf("\nEnter Address: ");
gets(applicant.address);
fflush(stdin);
printf("\nEnter Phone Number: ");
gets(applicant.phoneNumber);
fflush(stdin);
printf("\nEnter Email Address: ");
gets(applicant.emailAddress);
fflush(stdin);
printf("\nEnter Mobile Number: ");
gets(applicant.mobileNumber);
fflush(stdin);
*/
printf("\n\n| Education |");
printf("\nEnter Number of Subjects Written: ");
scanf("%d", &applicant.numSubjectsWritten);
fflush(stdin);
while (i < applicant.numSubjectsWritten)
{
printf("\nEnter the subject: ");
gets(applicant.csecSubjects[i]);
fflush(stdin);
printf("\nEnter the grade for that subject: ");
scanf(" %c", &applicant.grades[i]);
fflush(stdin);
i++;
}
return applicant;
}
int main(void)
{
FILE *file = fopen("Data.csv", "a+");
int i, j;
if (!file)
{
printf("\nError! Can not open data file.\nPlease contact the Program Addmission Manager as soon as possible with the error message.");
exit(1);
}
else
{
struct Applicant applicant = getApplicantData();
//fprintf(file, "%s:%s:%s:%s:%c:%s:%s:%s:%s", applicant.lastName, applicant.firstName, applicant.middleName, applicant.dateOfBirth, applicant.gender, applicant.address, applicant.phoneNumber, applicant.emailAddress, applicant.mobileNumber);
fprintf(file, "%s:%s:%s:", applicant.lastName, applicant.firstName, applicant.middleName);
for (i = 0; applicant.csecSubjects[i][0] != '\0'; i++)
{
fprintf(file, " %s", applicant.csecSubjects[i]);
fflush(stdout);
fflush(stdin);
fflush(file);
fprintf(file, " ( %c):", applicant.grades[i]);
fflush(stdout);
fflush(stdin);
fflush(file);
}
}
return 0;
}
First problems I see:
Remove the & from all instances where you scanf a string
Don't use gets, or mix scanf and fgets
Don't fflush(stdin)
Instead of scanf, consider using a custom-made input method with condition checking and anything you need. I will give an example.
#define BUFFER_SIZE 512
void input(char* buffer){
memset(buffer, 0, BUFFER_SIZE); // Initializing the buffer.
fgets(buffer, BUFFER_SIZE, stdin);
strtok(buffer,"\n");
}
How to take input using that?
void main(){
int username[BUFFER_SIZE];
input(username);
}
A way to write a structure to a file is shown below.
void Structure_Print(Applicant* applicant, FILE* stream, int no_of_applicant){
if(no_of_applicant==0){
fprintf(stdout, "No applicant yet.\n");
return;
}
fprintf(stream, "%s:%s:%s:", applicant.lastName, applicant.firstName, applicant.middleName);
for (i = 0; applicant.csecSubjects[i][0] != '\0'; i++)
{
fprintf(stream, " %s:", applicant.csecSubjects[i]);
fprintf(stream, " %c:", applicant.grades[i]);
}
return;
}
Also, I noticed how you tried to make it readable while saving it in subject(grade) format. I recommend you to not do that. Your .csv file is just for database. Nobody is going to read it. So just store the data by comma or any character separator. It will make it easier to extract data later.

using fgets with structure

I am trying to use fgets with structure, since I have to insert in character array. But when I use fgets it's not working properly. I can not enter value for the char array. Please help. Below is a sample program::
#include <stdio.h>
#include<string.h>
struct Student
{
int roll;
char name[50];
int age;
char branch[50];
char gender[1]; //F for female and M for male
};
int main()
{
struct Student s1;
printf("enter roll number of the student: ");
scanf("%d", &s1.roll);
printf("Enter student name: ");
fgets(s1.name, 50, stdin); // NOT WORKING ...
printf("Enter age number: ");
scanf("%d", &s1.age);
printf("Enter branch number: ");
scanf("%d", &s1.branch);
printf("Enter Gender: ");
scanf("%d", &s1.gender);
return 0;
}
First of all you need different format specifiers for different datatypes. So you need to use %c for a character and %[^\n] for a string containing spaces.
You also need to remove leading whitespaces before scanning a string, because a newline \n is left in the input buffer which would otherwise be read by %c and %[], as Weather Vane pointed out in a comment.
#include <stdio.h>
#include <string.h>
struct student
{
int roll;
char name[50];
int age;
char branch[50];
char gender; // can be a single character
};
int main(void)
{
struct student s1;
printf("Enter roll number: ");
scanf("%d", &s1.roll);
printf("Enter name: ");
scanf(" %49[^\n]", s1.name); // use %[^\n] to scan a string containing spaces
printf("Enter age: ");
scanf("%d", &s1.age);
printf("Enter branch name: ");
scanf(" %49[^\n]", s1.branch);
printf("Enter gender: ");
scanf(" %c", &s1.gender); // %c is the format specifier for a char
return 0;
}
fgets is not being bypassed, it's actually working as it should, what happens is that it reads the newline character that remains in the input buffer from the previous scanf, if you access s1.name you will see that it has a string ("\n\0") in it.
For name I have to insert space character too, so I used fgets
You can use scanf with [^\n] specifier which can read spaces. Mixing scanf with fgets is trouble, it can be done, but you should avoid it.
You should either use scanf only, or fgets only, in the latter case, if you need to convert strings to ints use sscanf or better yet strtol.
Your code has other issues, detailed in the comments with corrections:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Student
{
int roll;
char name[50];
int age;
char branch[50];
char gender; //F for female and M for male
};
For solution with scanf only it should, more or less, look like this:
void clear_buffer(){ // helper function to clear buffer
int c;
while((c = getchar()) != '\n' && c != EOF){}
if(c == EOF){
fprintf(stderr, "Fatal error!");
exit(EXIT_FAILURE);
}
}
int main()
{
struct Student s1;
printf("enter roll number of the student: ");
while (scanf("%d", &s1.roll) != 1){
fprintf(stderr, "Bad input, try again: ");
clear_buffer();
} // if bad input ask again
printf("Enter student name: "); // the space before % clears blanks
while (scanf(" %49[^\n]", s1.name) != 1){ // will read the line until
fprintf(stderr, "Bad input, try again: "); // enter is pressed, provided
clear_buffer(); // that it's not larger than 49
}
printf("Enter age number: ");
while(scanf("%d", &s1.age) != 1){
fprintf(stderr, "Bad input, try again: ");
clear_buffer();
}
printf("Enter branch number: ");
while (scanf(" %49[^\n]", s1.branch) != 1){ // branch is a string, %d
clear_buffer(); // specifier is for ints.
fprintf(stderr, "Bad input, try again: "); // note that I'm using width
} // limit (49) to avoid buffer overflow
printf("Enter Gender: ");
while(scanf(" %c", &s1.gender) != 1){ // only 1 character needed, use %c
fprintf(stderr, "Bad input, try again: ");
clear_buffer();
}
}
For a solution with fgets only which, I would argue is better, you can do something like this:
int main(){
struct Student s1;
char temp[50];
printf("enter roll number of the student: ");
if (fgets(temp, sizeof temp, stdin)){
if (sscanf(temp, "%d", &s1.roll) != 1){
fprintf(stderr, "Error parsing input!\n");
}
}
printf("Enter student name: ");
if (fgets(temp, sizeof temp, stdin)){
if (sscanf(temp, "%49[^\n]", s1.name) != 1){
fprintf(stderr, "Error parsing input!\n");
}
}
printf("Enter age number: ");
if (fgets(temp, sizeof temp, stdin)){
if (sscanf(temp, "%d", &s1.age) != 1){
fprintf(stderr, "Error parsing input!\n");
}
}
printf("Enter branch number: ");
if (fgets(temp, sizeof temp, stdin)){
if (sscanf(temp, "%49[^\n]", s1.branch) != 1){
fprintf(stderr, "Error parsing input!\n");
}
}
printf("Enter Gender: ");
if (fgets(temp, sizeof temp, stdin)){
if (sscanf(temp, " %c", &s1.gender) != 1){
fprintf(stderr, "Error parsing input!\n");
}
}
}
*scanf to parse ints still has a potencial flaw in case of overflow, there is no way of guarding against that, unless you use a more robust method like the aforementioned strtol.

Write to txt file

I have a problem when trying to write to a txt file. I'm making a car register and after adding 2 cars to it, it looks like this in the txt file.
BMW
Car
ABCD1234
Herald
45
Fiat
Car
QWER1234
RONALD
20
My wish is to get it on the same line like this.
BMW Car ABCD1234 Herald 45
Fiat Car QWER1234 RONALD 20
This is my "save" and "add vehicle" function atm.
int save(vehicle_t * v, int count)
{
FILE * f;
f = fopen("reg.txt", "w");
if(f == NULL)
{
printf("Could not open!\n");
return 0;
}
else
{
int i;
for(i=0; i < count; i++)
{
fprintf(f, "%s %s %s %s %d\n", v[i].brand, v[i].type, v[i].reg, v[i].owner.name, v[i].owner.age);
}
}
fclose(f);
}
void add_vehicle(vehicle_t *v, int count)
{
char brand[NSIZE]; char type[NSIZE]; char reg[NSIZE]; char name[NSIZE];
int age;
printf("Brand: ");
fgets(brand, NSIZE, stdin);
strcpy(v[count].brand, brand);
printf("Type of vehicle: ");
fgets(type, NSIZE, stdin);
strcpy(v[count].type, type);
printf("Reg number: ");
fgets(reg, NSIZE, stdin);
strcpy(v[count].reg, reg);
printf("Owners name: ");
fgets(name, NSIZE, stdin);
strcpy(v[count].owner.name, name);
printf("Owners age: ");
v[count].owner.age = num_check(MAX_AGE, MIN_AGE);
}
My thought is that fgets adds \n at the end of an array, but how do I get rid of this problem when working with structs?
fgets returns all the characters on the line including the trailing \n. To keep fprintf from printing the \n characters you can change the fprintf call to
fprintf(f, "%*.*s %*.*s %*.*s %*.*s %d\n",
strlen(v[i].brand)-1, strlen(v[i].brand)-1, v[i].brand,
strlen(v[i].type)-1, strlen(v[i].type)-1, v[i].type,
strlen(v[i].reg)-1, strlen(v[i].reg)-1, v[i].reg,
strlen(v[i].owner.name)-1, strlen(v[i].owner.name)-1, v[i].owner.name,
v[i].owner.age);
This instructs it to output all but the last character of each string.
You can put the terminator one position to the left with:
fgets(name, NSIZE, stdin);
name[strlen(name) - 1] = '\0';
or you could use scanf:
scanf("%100[^\n]%*c", name);
//but here I've put a maximum 100 chars inside the string,
//not using the variable
There are other ways to do that with scanf but if you are a beginner I'd recommend you to stick with one of the two options above.

Structure and fflush(stdin)

#include <stdio.h>
#include <stdlib.h>
struct Person{
char codeChacter[20];
char fullName[30];
int Iq, Eq;
};
int main(){
struct Person cha1, cha2;
printf("Enter detail of character 1: \n");
printf("code Character: ");
gets(cha1.codeChacter);
fflush(stdin);
printf("Full Name: ");
gets(cha1.fullName);
fflush(stdin);
printf("Enter Iq and Eq: ");
scanf("%d%d", &cha1.Iq, &cha1.Eq);
printf("Enter detail of character 2: \n");
printf("code Character: ");
gets(cha2.codeChacter);
fflush(stdin);
printf("Full Name: ");
gets(cha2.fullName);
fflush(stdin);
printf("Enter Iq and Eq: ");
scanf("%d%d", &cha2.Iq, &cha2.Eq);
printf("\n---------------------Detail------------------\n");
printf("%s\t%s\t%d\t%d\n", cha1.codeChacter, cha1.fullName, cha1.Iq, cha1.Eq);
printf("%s\t%s\t%d\t%d\n", cha2.codeChacter, cha2.fullName, cha2.Iq, cha2.Eq);
return 0;
}
This is my first program in structure. When I run this application, it work but it not right with my mind. So can you help me change my fail? Thanks very much!
scanf("%d%d", (pointers to read)); won't remove the newline character from the input stream, then the gets will read the newline character and won't work as "expected".
don't use fflush(stdin);, which is undefined behavior.
don't use gets(), which has risk of buffer overrun.
Possible fix:
#include <stdio.h>
#include <stdlib.h>
struct Person{
char codeChacter[20];
char fullName[30];
int Iq, Eq;
};
char* read_line(char *out, size_t bufsize){
char *lf;
if (fgets(out, bufsize, stdin) == NULL) return NULL;
/* remove the newline character because fgets store it while gets donesn't */
for (lf = out; *lf != '\0'; lf++){
if (*lf == '\n'){
*lf = '\0';
break;
}
}
return out;
}
int main(void){
char read_buf[1024];
struct Person cha1, cha2;
printf("Enter detail of character 1: \n");
printf("code Character: ");
read_line(cha1.codeChacter, sizeof(cha1.codeChacter));
printf("Full Name: ");
read_line(cha1.fullName, sizeof(cha1.fullName));
printf("Enter Iq and Eq: ");
read_line(read_buf, sizeof(read_buf));
sscanf(read_buf, "%d%d", &cha1.Iq, &cha1.Eq);
printf("Enter detail of character 2: \n");
printf("code Character: ");
read_line(cha2.codeChacter, sizeof(cha2.codeChacter));
printf("Full Name: ");
read_line(cha2.fullName, sizeof(cha2.fullName));
printf("Enter Iq and Eq: ");
read_line(read_buf, sizeof(read_buf));
sscanf(read_buf, "%d%d", &cha2.Iq, &cha2.Eq);
printf("\n---------------------Detail------------------\n");
printf("%s\t%s\t%d\t%d\n", cha1.codeChacter, cha1.fullName, cha1.Iq, cha1.Eq);
printf("%s\t%s\t%d\t%d\n", cha2.codeChacter, cha2.fullName, cha2.Iq, cha2.Eq);
return 0;
}
Using this code, you have to enter Iq and Eq in the same line and you can't insert any blank line before inputting Iq and Eq, while the program using scanf won't require that.

student info file handling

can you help me improve my code... this is all about student information... i'm having trouble in syntax... In editing menu... i try using strcmp but nothing happens, i first use fgets and store it at an array and then ask the user for an input and store it again in another array.. and then i'll compare... but it didn't work.. hope you can help me... this is my code..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student{
char name[30];
char id[8];
char course[5];
};
int main(void){
int option =0;
while(option!=6){
system("cls");
printf("Menu:\n");
printf("[1] Add Student.\n");
printf("[2] Display Student.\n");
printf("[3] Delete Student.\n");
printf("[4] Delete Student.\n");
printf("[5] Exit.\n");
scanf("%d",&option);
switch(option)
{
case 1:
addStudent();
break;
case 2:
displayinfo();
break;
case 3:
break;
case 4:
break;
default:
printf("That is not in the options!\nPlease Try again!\n");
break;
}
}
}
addStudent(){
int i;
FILE *stream = NULL;
stream = fopen("studentinfo.txt", "a+");
struct student s1;
struct student array[3];//here i wnt 2 apply d malloc but, still didn't know how 2start
for (i =0; i<1; i++){
printf("Enter Student ID: ");
scanf("%s", s1.id);
fflush(stdin);
printf("Enter Student Name: ");
gets(s1.name);
fflush(stdin);
printf("Enter Student Course: ");
scanf("%s", s1.course);
fprintf(stream, "\n%s,\t%s,\t%s", s1.id, s1.name, s1.course);
}
fclose(stream);
getch();
}
displayinfo(){
FILE *stream = NULL;
stream = fopen("studentinfo.txt", "rt");
char arr[100];
int i=0;
while(!feof(stream)){
fgets(arr, 100, stream);
printf("%s", arr);
}
fclose(stream);
getch();
}
here's my plan in EDITING MENU:
printf("enter details: ");
gets(arr2);
while(!feof(stream)){
fgets(arr, 100, stream);
if(strcmp(arr, arr2)==0){
//code here
}
}
will this work?
thanks guys hope you can help me ^_^
fgets() keeps the newline. gets() does not. Hence the strings will never match.
Try reading the manual for a function if you're not COMPLETELY sure what it is doing.
Instead of gets(arr2) try doing fgets(arr2, 100, stdin).
while(!feof(stream)){
fgets(arr, 100, stream);
use
while(fgets(arr, 100, stream) != NULL) {
...
}
if (ferror(stream))
printf("error in file" "\n");
feof() won't see an error while reading, so it may hang the loop

Resources