Reading from a text file

This is a discussion on Reading from a text file within the C Programming forums, part of the General Programming Boards category; I have a text file vidi1.txt. All this file has is a single number. I have written a C program ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    int main(void) - read FAQ

    backslash should be escaped:

    "c:\\tcc\\vidi1.txt"
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 02:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21