Thread: trouble reading data from a file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    trouble reading data from a file

    hi i am trying to read data from a file which is arranged as follows

    item1
    item2
    item3
    etc
    etc
    etc

    i am trying to read this then reproduce it on the console window line by line i have only been able to scan it all then display it all in one line in the console but i need to reproduce it line by line

    here is the code im using is there a way i can change it do do this? i am very new to c.

    the "if f(open) = fail" bit i stole straight out a text book by the way


    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void)
    {
      int i;
      FILE *fp; 
      
      if ((fp=fopen("datafile", "r")) == NULL)
        {
          fprintf(stderr, "Failed to open file\n");
          exit(1);
        } else 
          {
    	{
    	  char input[1000];
    	  rewind(fp);
    do
    	 { i = fscanf(fp, "%s", input);
    	  printf("%s\n", input);
    	 }
    while(i !=EOF);
    
    	}
    	fclose(fp);
          }
      return 0;
    }
    Last edited by bob56; 03-16-2005 at 03:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  3. Reading encrypted data from file
    By cctoombs in forum C Programming
    Replies: 2
    Last Post: 02-13-2005, 02:59 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM