mercredi 29 juin 2016

How to search for specific strings and integers in a .txt file C and use them?

This code is a small learning experience about File I/O which i can't quite finish yet:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    FILE *fP;

    char so_name[64];
    char user_name[64];
    int number;
    char continue_button;

    time_t t;

    system("TITLE Love-0-meter");
    system("COLOR 4D");

    srand((unsigned) time(&t));
    number = (rand() %100);

    printf("Hi and welcome to the love-o-meter, please fill in the details and see how much you and your SO"
       " love each other!nnHave you used this program before?(Y/N)");
    do
    {
        scanf(" %c", &continue_button);

    }while(continue_button != 'n' && continue_button != 'N' && continue_button != 'Y' && continue_button != 'y');

    system("CLS");
    printf("Enter your SO's name: ");
    scanf(" %s", so_name);
    printf("Enter your name: ");
    scanf(" %s", user_name);
    printf("n%s and %s, you love each other %d%%", so_name, user_name, number);

    if(continue_button == 'N' || continue_button == 'n')
    {
        fP = fopen("Love-o-meter.txt", "w");
    }
    else
    {
        fP = fopen("Love-o-meter.txt", "a");
    }
    fprintf(fP, "%s %s %in", so_name, user_name, number);
    fclose(fP);

    return 0;
}

Basically the user must enter 2 names in and be saved in a .txt file along with a random number. But before the names get saved the program must check if they've been used before in the same order. If they have indeed been used before, the program must not generate a new number but use the one saved together with the 2 names and print it out on the screen.

I've been reading all day but i just can't figure out how and what to implement to be able the program to read previously saved names and numbers and use them if it finds a match.

Aucun commentaire:

Enregistrer un commentaire