Thread: I/O Files

  1. #1
    Registered User
    Join Date
    Mar 2017

    I/O Files

    I need my program to read a text file that contains 10 names. Ask the user to enter a name, if the name exists in the file print “The name is found”, if the name is not there print “The name is NOT found”
    I have a word document with all the names saved as "lab" in the same file as my program.

    So far I have

    int main ()
        char *names;
        char name[20];
        FILE * fin, * fout;
        printf("Enter a name\n");
        scanf("%s", &name);
        fout = fopen("lab.txt", "r");
        if ((fout = fopen("lab.txt", "r")) == NULL)
        fprintf(fout, "%s", names);
        if (name == names){
            printf("Name is found");
            printf("Name is not found");
        return 0;
    but no matter what name I type it says "Name is not found"

  2. #2
    Registered User
    Join Date
    Jun 2015
    You are doing pretty much everything wrong.

    Don't call fopen twice. You can get rid of the first call (or just test fout in the if).

    Why is your input file called fout? Wouldn't fin make more sense?

    Why are you tryin to print to your input file with fprintf? And names is just an uninitialized pointer so it's an error to use it anyway.

    What do you think name == names is doing? Again, names is uninitialized, but even if it pointed to a string, that's not how you compare strings. Have you heard of strcmp?

    Anyway, you presumably want to open the file, read each name individually into a char array (so names, or let's call it name_in, should be an array, just like name), and compare it to name until you either find a match or reach the end of the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-30-2012, 03:25 PM
  2. Drag and Drop files/Read and write files
    By ScoutDavid in forum C Programming
    Replies: 2
    Last Post: 01-13-2011, 12:14 PM
  3. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  4. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  5. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM

Tags for this Thread