简体   繁体   中英

How do you open a FILE with the user input and put it into a string in C

So I have to write a program that prompts the user to enter the name of a file, using a pointer to an array created in main, and then open it. On a separate function I have to take a user defined string to a file opened in main and return the number of lines in the file based on how many strings it reads in a loop and returns that value to the caller.

So for my first function this is what I have.

void getFileName(char* array1[MAX_WIDTH])
{
  FILE* data;
  char userIn[MAX_WIDTH];
  printf("Enter filename: ");
  fgets(userIn, MAX_WIDTH, stdin);
  userIn[strlen(userIn) - 1] = 0;
  data = fopen(userIn, "r");

  fclose(data);
  return;
}

For my second function I have this.

int getLineCount(FILE* data, int max)
{
  int i = 0;
  char *array1[MAX_WIDTH];
  if(data != NULL)
  {
    while(fgets(*array1, MAX_WIDTH, data) != NULL)
    {
      i+=1;
    }
  }
  printf("%d", i);
  return i;
}

And in my main I have this.

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

#define MAX_WIDTH 144

void getFileName(char* array1[MAX_WIDTH]);
int getLineCount(FILE* data, int max);
int main(void) 
{
  char *array1[MAX_WIDTH];
  FILE* data = fopen(*array1, "r");
  int max;
  getFileName(array1);
  getLineCount(data, max);
  return 0;
}

My text file is this.

larry snedden 123 mocking bird lane
sponge bob 321 bikini bottom beach
mary fleece 978 pasture road
hairy whodunit 456 get out of here now lane

My issue is that everytime I run this I keep getting a 0 in return and I don't think that's what I'm supposed to be getting back. Also, in my second function I have no idea why I need int max in there but my teacher send I needed it, so if anyone can explain that, that'd be great. I really don't know what I'm doing wrong. I'll appreciate any help I can get.

There were a number of issues with the posted code. I've fixed the problems with the code and left some comments describing what I did. I do think that this code could benefit by some restructuring and renaming (eg array1 doesn't tell you what the purpose of the variable is). The getLineCount() function is broken for lines that exceed MAX_WIDTH and ought to be rewritten to count actual lines, not just calls to fgets.

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

#define MAX_WIDTH 144

/** 
 * Gets a handle to the FILE to be processed.
 * - Renamed to indicate what the function does
 * - removed unnecessary parameter, and added return of FILE*
 * - removed the fclose() call
 * - added rudimentary error handling.
 **/
FILE *getFile() 
{
      char userIn[MAX_WIDTH+1];
      printf("Enter filename: ");
      fgets(userIn, MAX_WIDTH, stdin);
      userIn[strlen(userIn) - 1] = 0; // chop off newline.
      FILE *data = fopen(userIn, "r");
      if (data == NULL) {
          perror(userIn);
      }
      return data;
}

/**
 * - removed the unnecessary 'max' parameter
 * - removed null check of FILE *, since this is now checked elsewhere.
 * - adjusted size of array1 for safety.
 **/
int getLineCount(FILE* data)
{
    int i = 0;
    char array1[MAX_WIDTH+1];
    while(fgets(array1, MAX_WIDTH, data) != NULL)
    {
        i+=1;
    }
    return i;
}

/** 
 * - removed unnecessary array1 variable
 * - removed fopen of uninitialized char array.
 * - added some rudimentary error handling.
 */
int main(void)
{
    FILE *data = getFile();
    if (data != NULL) {
        int lc = getLineCount(data);
        fclose(data);
        printf("%d\n", lc);
        return 0;
    }
    return 1;
}

There are several things I think you should repair at first:

  1. getFileName should help you getting the file name (as the name says), so in that function you shouldn't have both array1 and userIn (as a matter of fact array1 is not even used in the function, so it can be eliminated all togheter). The paramater and the file name should be 'the same'.

  2. data is a local FILE pointer, this means once you exit the function you lose it. My recommandation is to make it global, or pass it as an argument from the main class. Also do not close it 1 line after you open it.

  3. I guess the getLineCount is fine, but usually is a good practice to return and printf in main what is returned.

That max that is passed to the second function maybe to help you with the max size of a line? it might be.

Summing up, your getFileName should return the file name, so that userIn is what should be given by that parameter. The File opening should be done IN THE MAIN FUNCTION and be closed after everything you do related to the file, so at the end. Also, open the file after you get the name of the file.

Hopefully it helps you! Keep us tuned with your progress.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM