Reading words from a text file IN C

I need help with reading in text from a file. My final goal is to read in a text file and place each word of the text file into a linked list using dynamic memory (because the size of the file and words will vary between each). I have already decided on using a queue, because I will need to read back off each element in a first in first out manner, but I can not test anything/get any further until I can read in the file. I have tried a few things and the best I can get is the first word of each line. I havent used C for a while but for this case I need to get down and dirty and use C and am stuck at this point. Thanks for any help!

Comments

  • : I need help with reading in text from a file. My final goal is to read in a text file and place each word of the text file into a linked list using dynamic memory (because the size of the file and words will vary between each). I have already decided on using a queue, because I will need to read back off each element in a first in first out manner, but I can not test anything/get any further until I can read in the file. I have tried a few things and the best I can get is the first word of each line. I havent used C for a while but for this case I need to get down and dirty and use C and am stuck at this point. Thanks for any help!
    :
    If your definition of a word is a succession of letters, digits or printable characters without any blank (space, newline, tab), then you may use:
    [code]
    char buf[MAXLENGTH];
    FILE *f;

    f=fopen("My_text_file.txt","r");
    while (!feof(f))
    {
    fscanf(f,"%s",buf);
    /*Store the word elsewhere*/
    ...
    }
    fclose(f);
    ...
    [/code]

    Steph
  • : : I need help with reading in text from a file. My final goal is to read in a text file and place each word of the text file into a linked list using dynamic memory (because the size of the file and words will vary between each). I have already decided on using a queue, because I will need to read back off each element in a first in first out manner, but I can not test anything/get any further until I can read in the file. I have tried a few things and the best I can get is the first word of each line. I havent used C for a while but for this case I need to get down and dirty and use C and am stuck at this point. Thanks for any help!
    : :
    : If your definition of a word is a succession of letters, digits or printable characters without any blank (space, newline, tab), then you may use:
    : [code]
    : char buf[MAXLENGTH];
    : FILE *f;
    :
    : f=fopen("My_text_file.txt","r");
    : while (!feof(f))
    : {
    : fscanf(f,"%s",buf);
    : /*Store the word elsewhere*/
    : ...
    : }
    : fclose(f);
    : ...
    : [/code]
    :
    : Steph
    :

    the trouble with fscanf() is that is doesn't allocate memory. Hence, you'll scribble all over your primary memory, RAM etc., leading to some weird errors and program crashs. If you were using C++ you could use the string class and getline() function and tell it that the terminating charactor was ' ' rather than '
    '. Else, your going a have to use while() loop and realloc() but that may be tricky.
  • : : : I need help with reading in text from a file. My final goal is to read in a text file and place each word of the text file into a linked list using dynamic memory (because the size of the file and words will vary between each). I have already decided on using a queue, because I will need to read back off each element in a first in first out manner, but I can not test anything/get any further until I can read in the file. I have tried a few things and the best I can get is the first word of each line. I havent used C for a while but for this case I need to get down and dirty and use C and am stuck at this point. Thanks for any help!
    : : :
    : : If your definition of a word is a succession of letters, digits or printable characters without any blank (space, newline, tab), then you may use:
    : : [code]
    : : char buf[MAXLENGTH];
    : : FILE *f;
    : :
    : : f=fopen("My_text_file.txt","r");
    : : while (!feof(f))
    : : {
    : : fscanf(f,"%s",buf);
    : : /*Store the word elsewhere*/
    : : ...
    : : }
    : : fclose(f);
    : : ...
    : : [/code]
    : :
    : : Steph
    : :
    :
    : the trouble with fscanf() is that is doesn't allocate memory. Hence, you'll scribble all over your primary memory, RAM etc., leading to some weird errors and program crashs. If you were using C++ you could use the string class and getline() function and tell it that the terminating charactor was ' ' rather than '
    '. Else, your going a have to use while() loop and realloc() but that may be tricky.
    :
    fscanf does not allocate memory, but it is far from being a problem. You can allocate memory using malloc(), and if you use malloc() correctly (which is easy), you won't get any error. You do not have the choice: you have to allocate memory, even in C++. The fact is that the allocation may be hidden in C++ if you use a predefined class. However, it is still memory allocation.

    Steph
  • : : : : I need help with reading in text from a file. My final goal is to read in a text file and place each word of the text file into a linked list using dynamic memory (because the size of the file and words will vary between each). I have already decided on using a queue, because I will need to read back off each element in a first in first out manner, but I can not test anything/get any further until I can read in the file. I have tried a few things and the best I can get is the first word of each line. I havent used C for a while but for this case I need to get down and dirty and use C and am stuck at this point. Thanks for any help!
    : : : :
    : : : If your definition of a word is a succession of letters, digits or printable characters without any blank (space, newline, tab), then you may use:
    : : : [code]
    : : : char buf[MAXLENGTH];
    : : : FILE *f;
    : : :
    : : : f=fopen("My_text_file.txt","r");
    : : : while (!feof(f))
    : : : {
    : : : fscanf(f,"%s",buf);
    : : : /*Store the word elsewhere*/
    : : : ...
    : : : }
    : : : fclose(f);
    : : : ...
    : : : [/code]
    : : :
    : : : Steph
    : : :
    : :
    : : the trouble with fscanf() is that is doesn't allocate memory. Hence, you'll scribble all over your primary memory, RAM etc., leading to some weird errors and program crashs. If you were using C++ you could use the string class and getline() function and tell it that the terminating charactor was ' ' rather than '
    '. Else, your going a have to use while() loop and realloc() but that may be tricky.
    : :
    : fscanf does not allocate memory, but it is far from being a problem. You can allocate memory using malloc(), and if you use malloc() correctly (which is easy), you won't get any error. You do not have the choice: you have to allocate memory, even in C++. The fact is that the allocation may be hidden in C++ if you use a predefined class. However, it is still memory allocation.
    :
    : Steph
    :

    how do you suggest that this fella could get the size of the string before allocating the memory? The C++ string simply removes a lot of the code from the care of the programmer making his or her job eaiser. It'ld be impratical to use malloc() and realloc() to assign a string in a project simply because it adds additional tasks to the project. Hence, it is easier to use the string class.
  • : : : : : I need help with reading in text from a file. My final goal is to read in a text file and place each word of the text file into a linked list using dynamic memory (because the size of the file and words will vary between each). I have already decided on using a queue, because I will need to read back off each element in a first in first out manner, but I can not test anything/get any further until I can read in the file. I have tried a few things and the best I can get is the first word of each line. I havent used C for a while but for this case I need to get down and dirty and use C and am stuck at this point. Thanks for any help!
    : : : : :
    : : : : If your definition of a word is a succession of letters, digits or printable characters without any blank (space, newline, tab), then you may use:
    : : : : [code]
    : : : : char buf[MAXLENGTH];
    : : : : FILE *f;
    : : : :
    : : : : f=fopen("My_text_file.txt","r");
    : : : : while (!feof(f))
    : : : : {
    : : : : fscanf(f,"%s",buf);
    : : : : /*Store the word elsewhere*/
    : : : : ...
    : : : : }
    : : : : fclose(f);
    : : : : ...
    : : : : [/code]
    : : : :
    : : : : Steph
    : : : :
    : : :
    : : : the trouble with fscanf() is that is doesn't allocate memory. Hence, you'll scribble all over your primary memory, RAM etc., leading to some weird errors and program crashs. If you were using C++ you could use the string class and getline() function and tell it that the terminating charactor was ' ' rather than '
    '. Else, your going a have to use while() loop and realloc() but that may be tricky.
    : : :
    : : fscanf does not allocate memory, but it is far from being a problem. You can allocate memory using malloc(), and if you use malloc() correctly (which is easy), you won't get any error. You do not have the choice: you have to allocate memory, even in C++. The fact is that the allocation may be hidden in C++ if you use a predefined class. However, it is still memory allocation.
    : :
    : : Steph
    : :
    :
    : how do you suggest that this fella could get the size of the string before allocating the memory? The C++ string simply removes a lot of the code from the care of the programmer making his or her job eaiser. It'ld be impratical to use malloc() and realloc() to assign a string in a project simply because it adds additional tasks to the project. Hence, it is easier to use the string class.
    :
    I agree that it could be easier with C++ (details are hidden). However, it can be done in C too. Programming did not begin with C++ :-).
    It's not that hard to use malloc().
    I agree that you cannot know the size of the input before calling fscanf(), but it's far from being a problem (and anyway, this problem exists in C++ too). Either you suppose that words cannot be longer than XX bytes, or you read the file one character at a time, and you store them where you want. The second solution is a bit harder but will work with any input length.

    Steph
  • : : : : : : I need help with reading in text from a file. My final goal is to read in a text file and place each word of the text file into a linked list using dynamic memory (because the size of the file and words will vary between each). I have already decided on using a queue, because I will need to read back off each element in a first in first out manner, but I can not test anything/get any further until I can read in the file. I have tried a few things and the best I can get is the first word of each line. I havent used C for a while but for this case I need to get down and dirty and use C and am stuck at this point. Thanks for any help!
    : : : : : :
    : : : : : If your definition of a word is a succession of letters, digits or printable characters without any blank (space, newline, tab), then you may use:
    : : : : : [code]
    : : : : : char buf[MAXLENGTH];
    : : : : : FILE *f;
    : : : : :
    : : : : : f=fopen("My_text_file.txt","r");
    : : : : : while (!feof(f))
    : : : : : {
    : : : : : fscanf(f,"%s",buf);
    : : : : : /*Store the word elsewhere*/
    : : : : : ...
    : : : : : }
    : : : : : fclose(f);
    : : : : : ...
    : : : : : [/code]
    : : : : :
    : : : : : Steph
    : : : : :
    : : : :
    : : : : the trouble with fscanf() is that is doesn't allocate memory. Hence, you'll scribble all over your primary memory, RAM etc., leading to some weird errors and program crashs. If you were using C++ you could use the string class and getline() function and tell it that the terminating charactor was ' ' rather than '
    '. Else, your going a have to use while() loop and realloc() but that may be tricky.
    : : : :
    : : : fscanf does not allocate memory, but it is far from being a problem. You can allocate memory using malloc(), and if you use malloc() correctly (which is easy), you won't get any error. You do not have the choice: you have to allocate memory, even in C++. The fact is that the allocation may be hidden in C++ if you use a predefined class. However, it is still memory allocation.
    : : :
    : : : Steph
    : : :
    : :
    : : how do you suggest that this fella could get the size of the string before allocating the memory? The C++ string simply removes a lot of the code from the care of the programmer making his or her job eaiser. It'ld be impratical to use malloc() and realloc() to assign a string in a project simply because it adds additional tasks to the project. Hence, it is easier to use the string class.
    : :
    : I agree that it could be easier with C++ (details are hidden). However, it can be done in C too. Programming did not begin with C++ :-).
    : It's not that hard to use malloc().
    : I agree that you cannot know the size of the input before calling fscanf(), but it's far from being a problem (and anyway, this problem exists in C++ too). Either you suppose that words cannot be longer than XX bytes, or you read the file one character at a time, and you store them where you want. The second solution is a bit harder but will work with any input length.
    :
    : Steph
    :

    in C++ you could use std::getline() which returns a string bject for you to use if you desire it to do so. And it allocates the memory automagically saving the programmer lots o' effort.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories