Reputation: 71
I am trying to code a c program in file handling using a structure of students for adding information but in case 4 in switch when i try to input the record a 1st position it doesn't write the given details.The output however is correct for rest of the positions
#include<stdio.h>
struct stu
{
char fn[10];
char ln[10];
int id;
} student[10];
int main()
{
FILE *p1, *p2;
int n, idno, i, pos, choice;
char ch;
printf("\t\tSTUDENT DATA\n\n");
p1 = fopen("STUDENT.txt", "w");
printf("Student file opened\n");
printf("How many students? ");
scanf("%d", &n);
fflush(stdin);
printf("\nEnter data\n");
printf("ID First Name Last Name\n\n");
for (i = 0; i < n; i++)
{
fscanf(stdin, "%d %s %s", &student[i].id, student[i].fn,
student[i].ln);
fflush(stdin);
fprintf(p1, "%d %s %s", student[i].id, student[i].fn, student[i].ln);
}
fclose(p1);
printf("\nData written successfully!!\n");
printf("File closed!!\n");
do
{
printf(
"\nPlease enter one of the folowing choices for the required operation:\n");
printf("1.Reading information\n");
printf("2.Searching information\n");
printf("3.Copying information into new file\n");
printf("4.Adding information\n");
printf("5.Deleting information\n");
scanf("%d", &choice);
fflush(stdin);
switch (choice)
{
case 1:
printf("\nFile opening for reading....\n");
p1 = fopen("STUDENT.txt", "r");
for (i = 0; i < n; i++)
{
fscanf(p1, "%d %s %s", &student[i].id, student[i].fn,
student[i].ln);
fflush(stdin);
fprintf(stdout, "%d %s %s", student[i].id, student[i].fn,
student[i].ln);
printf("\n");
}
fclose(p1);
printf("File closed!!\n");
break;
case 2:
printf("\nEnter id no. to be searched for.... ");
scanf("%d", &idno);
fflush(stdin);
p1 = fopen("STUDENT.txt", "r");
i = 0;
while (i < n)
{
fscanf(p1, "%d %s %s", &student[i].id, student[i].fn,
student[i].ln);
fflush(stdin);
if (student[i].id == idno)
{
fprintf(stdout, "%d %s %s", student[i].id, student[i].fn,
student[i].ln);
break;
}
else
i++;
}
if (student[i].id != idno)
printf("ID not found!!\n");
fclose(p1);
break;
case 3:
printf("\n\nOpening file STUDENTC for the contents to be copied.....\n");
p1 = fopen("STUDENT.txt", "r");
p2 = fopen("STUDENTC.txt", "w");
printf("Copying contents.....\n");
for (i = 0; i < n; i++)
{
fscanf(p1, "%d %s %s", &student[i].id, student[i].fn,
student[i].ln);
fflush(stdin);
fprintf(p2, "%d %s %s", student[i].id, student[i].fn,
student[i].ln);
}
fclose(p1);
fclose(p2);
printf("Contents copied successfully!!\n");
printf("\nReading from newly created file.....\n");
p2 = fopen("STUDENT.txt", "r");
for (i = 0; i < n; i++)
{
fscanf(p2, "%d %s %s", &student[i].id, student[i].fn, student[i].ln);
fflush(stdin);
fprintf(stdout, "%d %s %s", student[i].id, student[i].fn,
student[i].ln);
printf("\n");
}
fclose(p2);
printf("\nAll operations successfully completed....:)");
printf("\nClosing and saving all opened files.....\n");
break;
case 4:
printf("At which position do you waht to insert the record?:\n");
for (i = 0; i < n; i++)
{
fscanf(p1, "%d %s %s", &student[i].id, student[i].fn,
student[i].ln);
fflush(stdin);
fprintf(stdout, "%d %s %s", student[i].id, student[i].fn,
student[i].ln);
printf("\n");
}
scanf("%d", &pos);
fflush(stdin);
p1 = fopen("STUDENT.txt", "a+");
for (i = n - 1; i >= pos - 1; i--)
{
student[i + 1] = student[i];
}
i++;
printf("\nEnter additional details:\n");
printf("ID First Name Last Name\n\n");
fscanf(stdin, "%d % s%s", &student[i].id, student[i].fn, student[i].ln);
fflush(stdin);
fprintf(p1, "%d %s %s", student[i].id, student[i].fn,
student[i].ln);
n++;
fclose(p1);
break;
case 5:
printf("\nEnter id no. to be deleted: ");
scanf("%d", &idno);
fflush(stdin);
for (i = 0; student[i].id != idno; i++)
{
if (i == n)
{
printf("Record not found!! :(\n");
break;
}
}
if (student[i].id == idno)
{
while (i != n)
{
student[i] = student[i + 1];
i++;
}
n--;
printf("Deleted successfully!! :)\n");
}
break;
default:
printf("Wrong choice entered!!!\n");
break;
}
printf("\nWant to run more operations?: ");
scanf("%c", &ch);
fflush(stdin);
} while (ch == 'y' || ch == 'Y');
return 0;
}
Upvotes: 1
Views: 688
Reputation: 5152
First of all, I don't think student[i+1]=student[i];
will work, you will need to
memcpy(&student[i+1], &student[i], sizeof(student[i]);
Secondly, you cannot insert
information into a text file, you will need to write the entire information out to a temporary file (say STUDENT.tmp
), delete the old STUDENT.txt
and rename STUDENT.tmp
to STUDENT.txt
Upvotes: 0
Reputation: 495
You have a typo in line 145:
fscanf(stdin,"%d % s%s",&student[i].id,student[i].fn,student[i].ln);
Should be:
fscanf(stdin,"%d %s %s",&student[i].id,student[i].fn,student[i].ln);
I discovered it by compiling it with extra warnings:
$ gcc -Wall -Wextra so.c
so.c: In function ‘main’:
so.c:145:4: warning: unknown conversion type character 0x20 in format [-Wformat]
so.c:145:4: warning: too many arguments for format [-Wformat-extra-args]
Upvotes: 1