hi,

i wanted some help in File handling in C. consider i have one File like a.txt
a.txt

[Marks]

Physics = 60
Chemistry = 80 ;Prac(25)
Maths = 98

[Grade]

Neatness = B
Attention = A ;A+(9.5)

Now suppose i have to insert new values in this file for the subjects or maybe for Grades, then what logic am i suppose to follow?

Do you want to insert one more record to the file or do you want to modify the current values? In any case please mention how you've approached the problem uptil now.

i want to modify the current values. i feel i should first read the file upto some size into a buffer then in that buffer search for Marks (strstr). after that agn search for Physics in the same way
please correct me if im wrong

You are right. Have you tried to code such functions? Please post them over here using code tags.

no i haven't and want help in this regard

You are surely not going to get anyone to write the entire code for you. You know how to do the task. Separate it into several small manageable modules. Write the algorithm in a logical order. Refering to each step in the algorithm, write the corresponding C language statements. After you've written the entire code, compile/run it. Try to debug the errors if any. If you are unable to do so, you will definitely get help from this forum. I don't see in what other way I can help you. Please mention if this is not what you were looking for. In that case you need to be more specific with the kind of help you want.

commented: Great advice :) +19

i am able to extract from file. For eg lets take [Marks] Physics = 60
i am able to extract upto =60, but nw my problem lies that i am not able to replace with new value. how do i do that?

Line 60 is wrong in your code, and lines 88-92 are close....

i havn't given any of the code. which code r u talking abt?

on what i know so far, u cant update the contents directly on to the file (well perhaps u can). Perhaps what i would suggest is read each line from the source file into a string. i.e line by line. See if u need to make some changes in that line. IF so do that changes in the string, and write that string back to a new file. And follow the same for all the lines in the source file and keep on copying it on to the new file. Once u have reached EOF on you old file. Delete the old file and rename the new file to the old filename.

That makes it easy for you.

ssharish

i havn't given any of the code. which code r u talking abt?

Ahh, you got the point -- maybe. But then again, maybe not.

commented: great indirectness :) +2

Is there any provision to delete some part of file?

Is there any provision to delete some part of file?

Well u could that through fseek function and try writing few bytes. But u need to be careful through.

ssharish

by trying the way as suggested by u, shall i use fputs to write the string to new file. for reading from the file and putting to string i am using fgets()

by trying the way as suggested by u, shall i use fputs to write the string to new file. for reading from the file and putting to string i am using fgets()

Spot on, thats right, use fgets to read from the old file. Alter the string and write the new string to the new file using fputs or fprintf if u wanted to format the string. This would be more approchable for inital.

ssharish

i thnk fprintf() is a better option

i thnk fprintf() is a better option

It depends up on how u want the write the data on to file. If u think fprintf is good use them.

ssharish

i think i nearly got it. thanks

a new problem ... when i am opening the new file it doesnot have anything. if i take to write only one parameter, like [MARKS] Physics=60
it will be written, but if i try to write multiple things it doesnot support. can you give me some sample code.

#include <stdio.h>

int main()
{
    FILE *read, *write;
    char Buffer[BUFSIZ];
    
    if( ( read = fopen("Marks.txt", "r") ) ==  NULL )
    {
        printf("Error: File cannot be opened\n");
        return 1;
    }
    
    if( ( write = fopen("TempMarks.txt", "a") ) ==  NULL )
    {
        printf("Error: File cannot be opened\n");
        return 1;
    }    
    
    if( fgets( Buffer, BUFSIZ, read) != NULL )
        fprintf( write, "%s", Buffer );
    
    fprintf( write, "%s - %s\n", "Name", "Name2" );
    
    while( fgets( Buffer, BUFSIZ, read) != NULL )
           fprintf( write, "%s", Buffer );
    
    fclose(read);
    fclose(write);
    
    remove("Marks.txt");
    rename("TempMarks.txt", "Marks.txt");

    return 0;
}

ssharish

ya thanks! but wht abt multiple entries? suppose i have checked the first entry in [MARKS] and want to check the next, how to proceed for that?

ya thanks! but wht abt multiple entries? suppose i have checked the first entry in [MARKS] and want to check the next, how to proceed for that?

Keep reading the file line by line using fgets. Check whether the read value is [MARKS]. Once you know that the read value if MARKS, you know what the MARKS tag will followed by, for example your modules marks. Continue reading the next line. And see if your have reached the line where the module you are looking for. Change the modules marks and continue reading untill you reach EOF.

And there you go you van change just as you can.

ssharish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.