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!

1
  • yes its possible. read this Commented Sep 13, 2014 at 12:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

7 Comments

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!
Use fgetc then...I'll update my answer
What about char var[100]; How to make it dynamically?
To allocate it dynamically,use malloc() like this : char *ch;ch=(char*)malloc(100*sizeof(char));
@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.
|
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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.