Thread: Reading from a text file

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Reading from a text file

    I have a text file vidi1.txt. All this file has is a single number. I have written a C program to read this file & display its contents. This does not give results as expected. Please help.

    Code:
    #include<stdio.h>
    
    void main()
    {
    	FILE *f1;
    	int i;
    	clrscr();
    	f1=fopen("c:\tcc\vidi1.txt","rb");
    	printf("Contents of file\n");
    	i=getw(f1);
    
    	fclose(f1);
    
    	printf("%d",i);
    	getch();
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int main(void) - read FAQ

    backslash should be escaped:

    "c:\\tcc\\vidi1.txt"
    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
    Oct 2001
    Posts
    2,934
    > i=getw(f1);
    I'm not sure what getw() does, but you can do the same thing with fscanf():
    Code:
    (if fscanf(f1, "&#37;d", &i) == 1)
    {
    	printf("%d", i);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM