-3

I need to read from an input file by using C programming language to do one of my assignments.

Here's my code:

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

int main(int argc, char *argv[])
{
    char *input = readFromFile(argv);
    return 0;
}

char *readFromFile(char *argv[])
{
    FILE *fp;
    fp = fopen(argv[1],"r");
    char *input, c;
    int i = 0;

    while(!feof(fp))
    {
        c = fgetc(fp);
        input[i++] = c;
    }
    fclose(fp);
    return input;
}

I want to do this reading operation in another function, not in function main(). I tried, but couldn't do it.

When I try to do it with the code above, I get an error message that says:

conflicting types for readFromFile()

How can I fix this error and do what I want?

casperOne
  • 73,706
  • 19
  • 184
  • 253
mikrobik
  • 1
  • 1
  • 3
  • You're dereferencing an uninitialized pointer (`input`)... This will crash. –  Nov 14 '12 at 17:47
  • `feof()` is plain wrong. Make sure you understand this. Don't put "C" on your CV until you do. – Kerrek SB Nov 14 '12 at 17:48
  • http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – William Pursell Nov 14 '12 at 17:48
  • 1
    Wood behind the mandate-arrow that in-flux with teaching the language, academia should be *required* to teach **debugging techniques**. This code never checks the result of `fopen()`, uses `feof()` incorrectly, writes through an uninitialized pointer into random memory, performs no boundary checking during reading, and utilizes assumed function-decl because there is no prototype before usage of `readFromFile()` in `main()`. – WhozCraig Nov 14 '12 at 17:54
  • When I tried reading from the text file with the same code in main() function, it worked. So, I thought there were no mistakes in my code in readFromFile() function. I'm gonna consider your answers, read and try to understand what you said about feof() function. Thank you... – mikrobik Nov 14 '12 at 18:05

2 Answers2

0

You have to declare readFromFile before using it. The best way to do this is to add a prototype:

char *readFromFile(char *argv[]); /* note that the identifier is useless here */

NB: By the way, there is a lot of other errors in your source code. The main one is that you don't allocate memory for input. Therefore, you will try to dereference an unitialized pointer: this leads to an undefined behavior. Since you are returning your pointer, you need to use dynamic allocation.

#include <stdlib.h>
char *input = malloc(SIZE);

Moreover, your utilisation of feof is wrong.

md5
  • 23,373
  • 3
  • 44
  • 93
-2

First of all you can choose between these:

1. Declare the function prototype;
2. Declare the function before the main.

This way the function is recognized in main.To be fast I always declare function before the main.In readFromFile you aren't allocating the memory that you need, fixed it for you:

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

char *readFromFile(char *argv[])
{
    FILE *fp;
    fp = fopen(argv[1],"r");
    char *input, c;
    int i = 0;
    size_t size=100*sizeof(char);
    input=(char*)malloc(size);

    while( (c = fgetc(fp)) != EOF )
    {
        if(++i == size)
        {
            size+= 100*sizeof(char);
            input=(char*)realloc(input,size);
        }
        input[i-1] = c;
    }
    fclose(fp);
    return input;
}

int main(int argc, char *argv[])
{
    char *input = readFromFile(argv);
    return 0;
}
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • Your reallocation strategy is not very efficient (BTW `sizeof(char)` and `malloc` cast are useless). – md5 Nov 14 '12 at 18:18
  • @Kirilenko My reallocation strategy is efficient speaking about CPU.You should know that realloc takes a lot of CPU, so instead of reallocating at every character, I allocate blocks of 100 Bytes.100 B are nothing in modern RAMs. About the cast, this is to silent the compiler warning, and sizeof(char) is because a char is not 1B on every machine.You should know this, every book taches this. – Ramy Al Zuhouri Nov 14 '12 at 20:47
  • @RamyAlZuhouri: You are wrong. 1) An efficient reallocation should use geometric progression. (check here: http://www.bourguet.org/v2/cs/realloc/). 2) The cast is useless in C, and it doesn't report any warning. You are maybe compiling with a C++ compiler. 3) `sizeof(char)` is **always** 1. Not always 8 bits, however (rather `CHAR_BIT`). Buy an other book. ;-) – md5 Nov 15 '12 at 16:06
  • The reallocation strategy depends on what you have to the file, you can't say that the geometric progression is the best strategy.If you have blocks of 100 B on your file then that's the best strategy and btw I can't reach your french file. However you were returning a memory allocated on the stack from a function, risking to overflow the buffer and having syntax error, then contest these details like casts (my compiler gives me a warning, then?) and efficiency.I think that this is simply ridicolous. – Ramy Al Zuhouri Nov 15 '12 at 19:42