i wanna learn how to add and delete records in a car stock inventory without having to delete the contents of the file and save it back. but i don't know how to. This is my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>
#include <time.h>





int Amountofcars;
char carnumber [4];
int fp;
int fp2;
int fpd;

char carname [30];
char Answer[4];
char borrowername[50];
char passworda[7];
char passwordb[7];
char passwordc[7];
char passwordd[7];

char usernamea[8];
char usernameb[8];
char usernamec[8];
char usernamed[8];





int database();
int  addcar();
int deletedatafile();
int report();
int exitprogram();


int main (void)
{
    char Yes[4] ;
    strcpy (Yes, "yes");
    char ppassword[7];
    char pusername[8];
    char password[7];
    char username[8];
    strcpy (password, "Rlando");
    strcpy (passworda, "Mytime");
    strcpy (passwordb, "Jonnie");
    strcpy (passwordc, "Micboy");
    strcpy (passwordd, "JesusC");
    strcpy (username, "hubert1");
    strcpy (usernamea, "Sharon4");
    strcpy (usernameb, "DonnaDe");
    strcpy (usernamec, "Mickymo");
    strcpy (usernamed, "Susan45");
    printf("Please enter your password then username\n");
    printf("Car Stock Administrators should note that passwords and usernames are case sensitive\n");
    scanf("%s", &ppassword);
    scanf("%s", &pusername); 
    if (strcmp(password,ppassword)==0 && strcmp(username,pusername)==0)
    {
        printf("Your password is valid\n");
        Sleep(2000);
        system("cls");
        database ();
    }       
    else if (strcmp(passworda,ppassword)==0 && strcmp(usernamea,pusername)==0)
    {
        printf("Your password is valid\n");
        Sleep(2000);
        system("cls");
        database ();        
    }
    else if (strcmp(passwordb,ppassword)==0 && strcmp(usernameb,pusername)==0)
    {
        printf("Your password is valid\n");
        Sleep(2000);
        system("cls");
        database ();
    }
    else if (strcmp(passwordc,ppassword)==0 && strcmp(usernamec,pusername)==0)
    {
        printf("Your password is valid\n");
        Sleep(2000);
        system("cls");
        database ();
    }
    else if (strcmp(passwordd,ppassword)==0 && strcmp(usernamed,pusername)==0)
    {
        printf("Your password is valid\n");
        Sleep(2000);
        system("cls");
        database ();
    }
    else 
    {
        printf("Your password and/or username is incorrect\n");
        Sleep(1000);
        system("cls");
        main ();
    }
    return 0;
}













int database ()
{
    printf("Please note that this program does not accept character spaces in text.\n");
    printf("There will be an error, instead use and underscore '_'  \n");
    printf("Also the format for report numbers and car numbers is 3 digits\n");
    Sleep(10000);
    system("cls");
    printf("Welcome to Systems Development's Car Stock database\n");
    int option;
    printf("Enter the option you need to carry out \n");
    printf(" 1 to add new car\n");
    printf(" 2 to delete contents of the file \n");
    printf("3 Write a report about a car \n");
    printf("4 to exit the program\n");
    scanf("%d", &option);
    while ((option >=1) && (option <=4))
    {
        if (option == 1)
        {
            system("cls");
            addcar ();  

        }
        else if (option == 2)
            {
                system("cls");
                deletedatafile();
            }
        else if (option ==3)
        {
            system("cls");
            report();
        }

        else if (option ==4)
            {
                system("cls");
                exitprogram();
            }






    }

    return 1;

}

int addcar()
{


    FILE *fp;
    fp = fopen("c:\\filedatabase.txt", "a+");
    int count;
    int numofcars;
    printf("Please enter the number of cars you want to add\n");
    scanf("%d", &numofcars);
    count =0;


    for (count=0;count < numofcars;count++)
    {
        printf("Enter the number of the car and then the car name to add below:\n");

        printf("Note that car  number has a maximum of 4 number spaces\n");
        scanf ("%s",carnumber);

        printf("Note that car name has maximum of  30 spaces\n");
        scanf ("%s",carname);

        time_t now;
        time(&now);
        fprintf(fp, "%s", ctime(&now));
        fprintf(fp," The vehicle added is: %s\n", &carname);
        fprintf(fp,"The car number is: %s \n\n", &carnumber);
        Amountofcars = Amountofcars+1;
    }
        fprintf(fp,"The amount of cars added to stock is: %d\n\n", Amountofcars);
        fclose(fp);
        Sleep(5000);
        system("cls");
        database();



    return 0;
}










int deletedatafile()
{

    char doption[4];
    char Deloption[4];
    strcpy (Deloption, "yes");
    printf("If You really wanna delete all the contents of the file type %s \n", &Deloption);
    scanf("%s", &doption);
    if (strcmp(doption, Deloption)==0)
    {
        FILE *fpd;
        printf("successful in deleting the contents of the data file \n");
        fpd =fopen("c:\\filedatabase.txt", "a+");
        time_t now;
        time(&now);
        fprintf(fpd, "%s", ctime(&now));
        fprintf(fpd, "Contents of this file was deleted\n");
        fclose(fpd);
        Sleep(1500);
        system("cls");
        database();


    }


    return 4;
}

int report()
{
    char Rcarname[30];
    char Rcarnum[4];
    char Reporte[500];
    int numofreports;
    int Amountofreports;
    int count;
    count =0;
    FILE *rpt;
    printf("Enter the number of reports you plan to file \n");
    scanf("%d", &numofreports);
    rpt = fopen("c:\\Car_Report.txt", "a+");
    for (count=0; count < numofreports; count++)
    {
        time_t now;
        time(&now);
        fprintf(rpt, "%s", ctime(&now));
        printf("Please enter the car name and number you wanna write a report on\n");
        printf("Note that car number has maximum of 4 spaces and for car name its 30\n");
        scanf("%s", &Rcarname);
        scanf("%s", &Rcarnum);
        Sleep(1000);
        printf("Now file your report on file below\n");
        printf("Use underscores where spaces in sentences should be\n");
        scanf("%s", &Reporte);
        fprintf(rpt,"The vehichle added is: %s \n", Rcarname);
        fprintf(rpt,"the car number is: %s \n\n", Rcarnum);
        Sleep(1000);
        fprintf(rpt,"A report was filed Re: %s \n");
        Amountofreports = Amountofreports+1;
    }
        fclose(rpt);
        Sleep(5000);
        system("cls");
        database();
        return 6;
}






int exitprogram()
{
    printf("the program can be closed by pressing enter in 5 seconds\n");
    printf("Endind Systems Development Car Stock \n");
    Sleep(5000);
    exit(1);
}

the short answer is: you can't.

the long answer is: you can, but only if all fields are consistently the same width, and there's never any variations in the numbers of fields or their sizes.

in which case, a solution can be where you'll use "fopen" to read, "fgets" to get each line, a string comparison such as "strstr" or "strtok" to find the field in question, a counter to track the position of where you want to change, then "fseek" to re-navigate back to the location in question, and "fwrite" to replace the text.

there's plenty of caveats and assumptions to consider, but if the number, size and position of your fields are immutable, you can take a stab at it and see what happens.

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.