Thread: Reading functions

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    Reading functions

    How would I begin to write a function that reads a data file (text) in and split it up into separate words?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Ok I don't understand any of that. Thanks though.

  4. #4
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    If you post what you've got so far, and explain where you're stuck, we can help you out.
    Jason Deckard

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Haven't got anywhere so far. I've just been told I need to create a function that reads in data from a text file.

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    It's not too late to switch majors.
    Jason Deckard

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Let see if this help you
    Code:
    Delcare a file pointer <-- FILE * fp;
    Open a file <-- Use fopen Ex: fp = fopen("test.txt","r");
    
    While ( fgets(buf, sizeof buf,fp) != NULL)
    /* this will read the file into the buffer line by line. */
    Will that be able you to start it up. And note u havn't done any error checking. I will leave it to you.

    this would help you
    Files I\O

    ssharish2005

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    The following works to read in the text file but also reads in punctuation, is there any way to exclude them?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main(void)
    {
      FILE *fp = fopen("file.txt", "r");
      char result[128];
      
      while (!feof(fp))
      {
        if (fscanf(fp, "%128s", result)<1)
        {
          break;
        }
        printf("Found word bit: %s \n", result);
      }
      fclose(fp);
    }

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can use "%128[^, ]" format to read till the first char in the [] after ^ is encountered.
    this char is not read, so you should in some way skip this chars till the new word start is encountered

    easier to achive - use fgets to read the whole line and then sscanf to parse words
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    strspn() and strcspn() may prove useful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. Variables do not equal functions!!!
    By me@burk. in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 06:24 AM