Thread: Reading text

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Reading text

    How do I create a loop which will go to each line and read each line in a text file? wat's the syntax like?
    Only by the cross are you saved...

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    also, i did find a tutorial in this site about File I/O but the tutorial was suited for C++ and not for C, anybody know of a good tutorial online for this purpose?
    Only by the cross are you saved...

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I didn't try searching Cprogramming.com, but I'm sure it's been asked and answered here. But this example did come to mind.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    just a basic question :

    wat's the difference between char* and char?
    Only by the cross are you saved...

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    for example, wat does this do:

    char* newline = strchr(str, '\n');
    if(newline)
    {
    *newline = '\0'; /* strip off trailing '\n' */
    }
    Only by the cross are you saved...

  6. #6
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    strchr returns a pointer to somewhere inside of the string in question if the value is found. So if the string is "test\n"
    Code:
    char *newline = strchr(str, '\n');
    will return a pointer to the '\n' in the string. Since it's a pointer, you can change what it points to and the string will change too. So *newline = '\0'; changes the '\n' to '\0' through the pointer.

    Now, since strchr might not find the value it's looking for, you have to check for NULL, the value that is returned if no match is found. That's where the if statement comes in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text
    By Anator in forum C++ Programming
    Replies: 33
    Last Post: 01-30-2008, 12:13 PM
  2. reading a char at a time from text
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2008, 12:27 PM
  3. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 12:51 PM
  4. Reading text file and structuring it..
    By Killroy in forum C Programming
    Replies: 20
    Last Post: 11-19-2004, 08:36 AM
  5. Reading Tab Separted Text files
    By Cathy in forum C Programming
    Replies: 1
    Last Post: 02-15-2002, 10:28 AM