Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct
{
char studentName[50];
int studentId;
} database;
int main(int argc, char *argv[])
{
database school;
FILE *file_ptr;
char text[50];
int data,temp;
while(1)
{
printf("\nEnter Student Name: ");
gets(school.studentName);
printf("\nEnter Student ID: ");
scanf("%d", &school.studentId);
if(school.studentId == 0)
{
break;
}
printf("\nAdd to school.csv? (y/n): ");
getchar();
scanf("%c", &temp);
if(temp == 'y' || temp == 'Y')
{
file_ptr = fopen("school.csv" , "a");
printf("\nFile school.csv opened\n");
fprintf(file_ptr, "%s, %d\n", school.studentName , school.studentId);
fclose(file_ptr);
printf("File Saved.\n");
}
else(temp == 'n' || temp == 'N');
{
printf("\nFile not Saved.");
}
}
system("PAUSE");
return 0;
}
Fixed but how do i fix my fprint?
How do I fix this line so I can use a Space (ie. Bobby Bob)
Code:
printf("\nEnter Student Name: ");
scanf("%s", &school.studentName);
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct
{
char studentName[50];
int studentId;
} database;
int main(int argc, char *argv[])
{
database school;
FILE *file_ptr;
char text[50];
int begin,temp;
while(1)
{
printf("\nInput Data? (Yes = 1) : ");
scanf("%d", &begin);
if(begin != 1)
{
break;
}
printf("\nEnter Student Name: ");
fgets(school.studentName, 50, stdin);
printf("\nEnter Student ID: ");
scanf("%d", &school.studentId);
printf("\nAdd to school.csv? (y/n): ");
getchar();
scanf("%c", &temp);
if(temp == 'y' || temp == 'Y')
{
file_ptr = fopen("school.csv" , "a");
if( file_ptr != NULL)
{
printf("File school.csv opened\n");
}
//printf("\nFile school.csv opened\n");
fprintf(file_ptr, "%[^ ]s, %d\n", school.studentName , school.studentId);
fclose(file_ptr);
printf("File Saved.\n");
}
else(temp == 'n' || temp == 'N');
{
printf("\nFile not Saved.\n");
}
}
system("PAUSE");
return 1;
}
Problems:
1. Skipping Student Name Input
2. Fprintf not saving to file