1

I am working on a program to write user input to a file and then search for a specific record in the file and output it to the screen.

I tried using fgets and also fputs, but I haven't been successful. Here's what I have so far.

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

main ()
{
    FILE *fileptr;
    char id [30];
    char name [47];
    char amt[50];

    fileptr = fopen("C:\\Users\\Andrea\\Documents\\Tester.txt", "w");
    if (fileptr == NULL) {
        printf("File couldn't be opened\n\a\a");
        fclose(fileptr);
        exit(0);
    }

    printf("Enter name: \n");
    fscanf(fileptr, "%c", name);
    fputs(name, fileptr);
    fclose(fileptr);
    printf("File write was successful\n");
    return 0;
}
2
  • 1
    Okay. That program won't read user input, write that to a file or output any record to the screen. Where is it failing for you, and also, what is the format of the Tester.txt file? Commented Nov 14, 2011 at 0:23
  • possible duplicate of Writing user input to a file in C Commented Nov 14, 2011 at 0:25

2 Answers 2

2

Use:

fscanf(stdin, "%s", name);

But better still, use scanf instead, as kol mentioned. This is because scanf() is designed to read the user response from the screen while fscanf() is for scanning from any input streams (which are usually files).

And the statement should be reading from the screen (stdin), not from the file (which was opened as "write" only).

Sign up to request clarification or add additional context in comments.

Comments

0

Use scanf to read user input, and fprintf to write it to the file. Then use fscanf to read from the file, and printf to display what you have read. See cplusplus.com for the details and sample code.

EDIT:

Here is an example (please run the executable from the command line):

#include <stdio.h>
#include <string.h>

int main()
{
  FILE *file;
  int i;
  char firstName[32];
  char lastName[32];
  int found = 0;

  // Open the file for writing
  file = fopen("records.txt", "wt");
  if (!file)
  {
    printf("File could not be opened\n\a\a");
    getchar();
    return -1;
  }

  // Read and save data
  for (i = 0; i < 3; ++i)
  {
    // Read data
    printf("Record #%d\n", i + 1);
    printf("Enter first name: "); scanf("%s", firstName);
    printf("Enter last name:  "); scanf("%s", lastName);
    printf("\n");

    // Save data
    fprintf(file, "%s\t%s\n", firstName, lastName);
  }

  // Close the file
  fclose(file);

  // Open the file for reading
  file = fopen("records.txt", "rt");
  if (!file)
  {
    printf("File could not be opened\n\a\a");
    return -1;
  }

  // Load and display data
  i = 0;
  while(!feof(file) && !found)
  {
    ++i;
    fscanf(file, "%s\t%s", firstName, lastName);
    if (strcmp(firstName, "John") == 0 && strcmp(lastName, "Doe") == 0)
    {
      printf("Record found (#%d): %s %s\n", i, firstName, lastName);
      found = 1;
    }
  }
  if (!found)
    printf("Record could not be found");

  // Close the file
  fclose(file);

  return 0;
}

6 Comments

this needs to be done to get 10 records, could a for loop work?
@user1 Sure. Open the file with fopen, run the loop to get each record, then close the file with fclose.
where would be the best possible spot to put the for loop?
I put it right after i opened the file and before i start getting the input
@user1 That's OK. Don't forget to close the file when you finish reading and saving user input (that is, right after the for loop).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.