0

I wrote c code which input value for my program comes from here :

char *input[] = {"This input string value !!!", NULL};

But how can I read this value from the file (e.g. input.txt)? Is it possible to get the file content like a string?

Thanks a lot!

Umidjon Urunov
  • 651
  • 1
  • 17
  • 39

3 Answers3

2

If you want to read a file line-by-line, the easiest way to go is using getline. Read the man page for a detailed description and a good code example.

getline will do all the low-lvel plumbing work of allocating buffers, copying data and scanning for newline characters, etc for you. Keep in mind that this is only possible since getline uses dynamically allocated memory that you'll need to free again.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
0

You can use fgets() like this:

#include <stdio.h>

int main(void)
{
    char buffer[100];

    FILE *file = fopen("input.txt", "r");
    // Checks if the file was opened successfully
    if (file == NULL)
    {
        fputs("Failed to open the file\n", stderr);
        return -1;
    }

    // fgets here reads an entire line or 99 characters (+1 for \0) at a time, whichever comes first
    while (fgets(buffer, sizeof(buffer), file) != NULL)
    {
        printf("Line read = %s\n", buffer);
    }

    fclose(file);
}

You can also use fgetc() like this:

#include <stdio.h>

int main(void)
{
    int ch;

    FILE *file = fopen("input.txt", "r");
    // Checks if the file was opened successfully
    if (file == NULL)
    {
        fputs("Failed to open the file\n", stderr);
        return -1;
    }

    // fgetc reads each character one by one until the end of the file
    while ((ch = fgetc(file)) != EOF)
    {
        printf("Character read = %c\n", ch);
    }

    fclose(file);
}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • thanks for answer, there one problem you gave limited number of elements for array how can do it not limited because I don't know the size f content it will be different every time. The next thing is fgets(var, 50, file); 50 is limit of read character? How can I make it without any limitations? Thanks! – Umidjon Urunov Sep 13 '14 at 12:52
  • Use `fgetc` then...I'll update my answer – Spikatrix Sep 13 '14 at 12:56
  • What about char var[100]; How to make it dynamically? – Umidjon Urunov Sep 13 '14 at 12:58
  • To allocate it dynamically,use `malloc()` like this : `char *ch;ch=(char*)malloc(100*sizeof(char));` – Spikatrix Sep 13 '14 at 13:03
  • The value in the call to `fgets` does *not* limit the string length that you can read with it. Read its [description](http://linux.die.net/man/3/fgets). – Jongware Sep 13 '14 at 13:23
  • Why do I get Segmentation fault (core dumped)? – Fred Oct 16 '20 at 03:17
  • 2
    @JoanaF. I've edited the code in the answer. Try it out. If you still have the crash issue, post a new question in Stack Overflow. – Spikatrix Oct 16 '20 at 08:41
0

On recent Posix compliant systems you could use getline(3), something like

FILE *fil = fopen("somefile.txt", "r");
if (!fil) {perror("somefile.txt"); exit(EXIT_FAILURE); };
char*linbuf = NULL; 
size_t siz = 0;
ssize_t linlen = 0;
while ((linlen=getline(&linbuf, &siz, fil))>0) {
   // linbuf contains the current line
   // linlen is the length of the current line
   do_something_with(linbuf, linlen);
};
fclose(fil);
free(linbuf), linbuf=NULL;
linlen = 0, siz = 0;
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547