Thread: Reading in a strand of text.

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    Reading in a strand of text.

    I am currently writing a program to read lines from a .txt file into my C program. So far I can get the program to read the first line but no other ones. Can someone please tell me the command to read more lines as I've searched the internet and i can find anything to help.

    This is my program so far...
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    void	READFILE__B();
    
    char	StringStore[50], Line[100];
    
    void main(void)
    {
    	clrscr();
    	printf("Enter you Name and College Number:\n\n");
    	scanf("%s",StringStore);
    	printf("\n");
    	getch();
    	READFILE__B();
    }
    //***************************************************
    void READFILE__B()
    {
    	FILE *Data;
    
    	Data = fopen("D:\\EXAMC.TXT","r");
    	fgets(Line, 100, Data);
    	printf("%s", Line);
    	getch();
    }
    //***************************************************
    The part where I'm having trouble is the line that says
    Code:
    printf("%s", Line);
    It will read only 1 and i need to read about 4.

    Any help would be much appreciated.

    Dean

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> fgets(Line, 100, Data);

    Put that in a loop methinks.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - Having empty parameters means potentially different things. If you want a function to be described as taking no arguments, make the argument list void
    2 - main returns an integer, not void.
    3 - You can avoid using global variables, and generally should.
    4 - You are reopening the file every time you call the function.
    5 - You read the first line every time you reopen it.
    6 - You never close the function.

    In short, you need a loop. All you ever do is read one line. You can just pass the opened file pointer to your read function, and read a line. Then just call your read function over and over. There are many ways to do this.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    2
    Thanks for your help, I'll try it when i can and post whether it worked or not.

    Cheers,

    Dean

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