Click here to Skip to main content
14,268,370 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have written this program in C. Data Accepting and showing part is working fine. But when i am trying to search a record and modify it with new data, it is not working.

#include <stdio.h>
#include <string.h>
int main()
{
    int tf=0;
    int i=0;
    int j=0;
    char temp[20];
    FILE *fw,*fr;
    char opsn='y';
    char *p;
    int count=0;
    //for writing records into file
    struct student
    {
        char name[20];
        int age;
    }s,s2;
    int recsize=sizeof(s);
    int taka;
    char stnm[20];
    char me[20];
    int ge;
    printf(" \n 1 add ");
    printf(" \n 2 show ");
    printf(" \n 3 modify");
    scanf("%d",&taka);
    if(taka==1)
    {
        fw=fopen("op.txt","a");
        while(opsn=='y')
        {
            count++;
            printf("Enter student name and age\n");
            scanf("%s%d",&s.name,&s.age);
            fprintf(fw,"%s\t%d\n",s.name,s.age);
            printf("Want another record(y|n)\n");
            fflush(stdin);
            opsn=getche();
        }
        fclose(fw);
    }
    if(taka==2)
    {
        //for reading records rom file
        fr=fopen("op.txt","r");
        printf("\nRecord from file is\n");
        i=0;
        while(fscanf(fr,"%s\t%d\n",s.name,&s.age)!=EOF)
        {
            printf("%s %d\n",s.name,s.age);
        }
    }
    //for modifying records rom file
    if(taka==3)
    {
        fr=fopen("op.txt","r+");
        printf("\nEnter name of student to modify");
        scanf("%s",&stnm);
        printf("ok");
        rewind(fr);
        while(fscanf(fr,"%s\t%d\n",&s.name,&s.age)!=EOF)
        {
            if(strcmp(s.name,stnm)==0)
            {
                printf("\nenter new name,age");
                scanf("%s%d",&me,&ge);
                strcpy(s.name,me);
                s.age=ge;
                fseek(fr,recsize,SEEK_SET);
                fprintf(fr,"%s\t%d\n",s.name,s.age);
                break;
            }
            recsize = recsize * tf;
            printf("%s %d  %d\n",s.name,s.age,tf);
            tf++;
        }
        printf(" Done ");
    }
}


What I have tried:

tried with fread() and fwrite()
it is working.

But i want to do do same file updation program using
fprintf()
fscanf()
Posted
Updated 16-Jul-19 15:50pm
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

Improve your code also by checking the return codes like from fprintf.

You missed a fclose call at the end.

PS: your code looks like "scribbled from a code monkey" ;-)
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

I'm not wading through that: it's unindented or badly indented, undocumented, uncommented, uses "lazy" variable names, and monolithic.

Start by indenting it correctly, so it's easier to read: pick an indentation style, and be consistent. It doesn't matter which you chose: K&R. Whitesmiths, even the execrable 1TB will do - just be consistent.
Then rename variables so that they reflect what they are used for: s2, me, ge, i, j ... are just easy to type, they don't help you understand the code, and so they can easily cause problems by using the wrong variable. index, userInput, and so on may be longer to type, but they help the code become self documenting and more reliable.
Then break it up into functions: instead of inline code in a single function, write an InsertRow function, and call it from your main, the same with DisplayRow and DisplayAllRows. Then create an UpdateRow function and call that as well.

THen you can start fixing whatever the heck is wrong with it: we have no idea and "it is not working" tells us absolutely nothing. We have no idea what data you had before, what you did to it, and what you got! So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
Stefan_Lang 16-Jul-19 7:39am
   
" K&R. Whitesmiths, even the execrable 1TB "
I had to look it up because I haven't heard of '1TB' elsewhere, but according to wikipedia K&R and 1TBS are the same (assuming you do mean 1TBS - all reference to '1TB' lead to pages talking about memory). I know them under the name 'egyptian style' or 'egyptian brackets' which I consider a much more self-documenting name ;-)
OriginalGriff 16-Jul-19 8:00am
   
1TB always uses a bracket, even when it isn't necessary, K&R doesn't.

What I meant to say was "Allman" rather than K&R, but it was early in the morning and my brain wasn't fully working ...
Stefan_Lang 16-Jul-19 8:25am
   
I see. Wikipedia doesn't make that distinction for the styles in general, only as a variant. see https://en.wikipedia.org/wiki/Indentation_style

According to that page, Allman is different to K&R in that it places the opening bracket on a new line.

Personally, I've been using the Stroustrup style, mostly because Stroustrup's is the C++ book I used to reference most of the time. Now I'm relying on Clang-Tidy to do the formatting for me and it's set to use Allman's style.
Rate this:
Please Sign up or sign in to vote.

Solution 3

One of my friend helped me.

Solution-
if(taka==3)
{
fr=fopen("op.txt","r");
ft=fopen("opt.txt","w");

printf("\nEnter name of student to modify");
scanf("%s",&stnm);

rewind(fr);
while(fscanf(fr,"%s\t%d\n",&s.name,&s.age)!=EOF)
{

if(strcmp(s.name,stnm)==0)
{
printf("\nenter new name,age");
scanf("%s%d",&me,&ge);
strcpy(s.name,me);
s.age=ge;
fprintf(ft,"%s\t%d\n",s.name,s.age);
}
else
{
fprintf(ft,"%s\t%d\n",s.name,s.age);
}


}

fclose(fr);
fclose(ft);
fr=fopen("op.txt","w");
ft=fopen("opt.txt","r");
while(fscanf(ft,"%s\t%d\n",&s.name,&s.age)!=EOF)
{
fprintf(fr,"%s\t%d\n",s.name,s.age);
}
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100