Question:
C language function HELP PLEASE?
Anonymous
2012-02-14 21:44:17 UTC
Here is my program so far:
#include
#include
#include
#include

#define MAX 81
FILE *openFile(char fileName[]);
int readLine(char str[],FILE *inputFile);

main()
{
char fileName[MAX] = "";
FILE *inputFile = openFile(fileName);

char str[MAX];
readLine(str, inputFile);

return;
}

FILE *openFile(char fileName[]) //Function A
{
FILE *inputFile;

printf("Please enter a filename.\n");
scanf("%s", fileName);

if (!(inputFile = fopen(fileName, "r")))
{
printf("\aCould not open the input file.\n");
exit (100);

} // if

return(inputFile);
}

int readLine(char str[], FILE *inputFile)
{

char str[MAX];
FILE *inputFile;

fgets(str, sizeof (str), inputFile);
printf("Here is your string: \n\t%s", str);

return;

}

-I am trying to get my second function to work, but cannot get it done. SOMEONE PLEASE HELP ME!

Here is what readLine is supposed to do:
Create a function that reads one line into a string (assuming the file is already open), the return should be an int or bool (true if a string was successfully read, false if end-of-file). In addition to the string to read into, you may include the physical size of the string as a parameter. When reading into the string, read to the end of line ('\n') using fgets, then remove the '\n' from the end of the string.

-Thank you for your time!!!
Three answers:
McFate
2012-02-14 21:49:43 UTC
You shouldn't re-declare your arguments as variables in the function body. That duplicate declaration doesn't do what you want:



int readLine(char str[], FILE *inputFile) {

char str[MAX]; // delete this line

FILE *inputFile; // delete this line



Otherwise, you just need to capture and return the value that fgets() returns.



@M
anonymous
2012-02-15 06:20:17 UTC
#include

#include /* For EXIT_FAILURE */



static int readline(char *s, size_t size, FILE *stream);



static FILE *openfile(void)

{

        char path[1024];

        FILE *stream;



        for (;;) {

                printf("Enter a filename: ");

                fflush(stdout);

                if (readline(path, sizeof path, stdin) == EOF) {

                        printf("\n");

                        return 0;

                }

                stream = fopen(path, "r");

                if (stream)

                        break;

                fprintf(stderr, "Unable to open file \"%s\"\n", path);

        }

        return stream;

}



int main(void)

{

        FILE *stream;

        char buffer[1024];



        stream = openfile();

        if (!stream)

                return EXIT_FAILURE;

        if (readline(buffer, sizeof buffer, stream) == EOF)

                return EXIT_FAILURE;

        printf("Read \"%s\"\n", buffer);

        return 0;

}



/* Handles "\n", "\r", and "\r\n" line terminators. */

static int eol(int c, FILE *stream)

{

        if (c == '\n')

                return 1;

        if (c == '\r') {

                c = getc(stream);

                if (c == '\n')

                        return 1;

                ungetc(c, stream);

                return 1;

        }

        return 0;

}



int readline(char *s, size_t size, FILE *stream)

{

        int c;



        size--;

        while ((c = getc(stream)) != EOF && !eol(c, stream))

                if (size) {

                        *s++ = c;

                        size--;

                }

        *s = '\0';

        return c;

}
anonymous
2012-02-15 05:45:26 UTC
Try IRC. Lots of people on there are willing to help.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...