Hi, I'm a newbie C programming student, so please note that my program may look silly and simple to you and my question may be stupid.
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define MAX 81
FILE *fileOpen(char fileName[]);
int readFile();
int main (void)
{
char fileName[MAX] = "";
FILE *inputFile;
fileOpen(fileName);
readFile();
return 0;
}
FILE *fileOpen(char fileName[]) //function A
{
FILE *inputFile;
printf("Please enter a file name.\n");
scanf("%s", fileName);
if (!(inputFile = fopen("%s", "r")))
{
printf("\aCould not open the input file.\n");
exit (100);
} // if
return (inputFile);
}
int readFile() //function B
{
FILE* inputFile;
char strng[MAX];
char* pStrng = strng;
int len;
fgets(strng, sizeof(strng), inputFile);
len = strlen(strng);
if (strng[len - 1] == '\n'){
strng[len - 1] = '\0';
}
while(fgets(strng, sizeof(strng), inputFile)){
fputs(strng, stdin);
return 1;
}
return 0;
return;
}
-This code does compile, but my first function is not working correctly. It always returns the error message "Could not open the input file". Even if I type in a valid file... Please let me know how to fix it and any other mistakes that you can catch.
NOTE: This is by no means a complete program, its just the beginning of what I could get done so far.
Here is what the second function 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.
-Let me know if you can't figure out what my first one is supposed to do.
-THANK YOU FOR YOUR TIME!