Thread: Beginner: I/O using .txt files

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    Beginner: I/O using .txt files

    Hello, I am writing a program to read characters from a txt file, but initially asking the user via the console to input the file name. this is what i've come up with so far but in unsure how to declare the file name at the pointer as a user in putted value.

    Code:
    #include<stdio.h>
    
     int main(void)
     {
    	char buffer[256];
    	char file[14];
    
    	printf("Please enter the file name you want to read: ");
    	scanf("%s \n", &file);
    
        FILE *file_pointer=NULL;
    	file_pointer=fopen("%s", "r");
    
    	while (fgets(buffer,256,file_pointer));
    	{
    		printf("%s",buffer);
    	}
     }
    Regards
    Ryan

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You need to look up sprintf().

    Also, don't forget that main returns 0 at the end.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    #include<stdio.h>
    
     int main(void)
     {
    	char buffer[256];
    	char file[14];
    
    	printf("Please enter the file name you want to read: ");
    	scanf("%13s", file); /* read max. 13 chars -> no buffer-overrun here */
    
            FILE *file_pointer;
    	file_pointer=fopen(file, "rt"); /* rt (read text) is the same as r, but more readable */
            if( !file_pointer ) {perror(file); exit(1);} /* error-handling and error info to console */
    
    	while (fgets(buffer,256,file_pointer));
    	{
    		printf("%s",buffer);
    	}
            fclose(file_pointer);
     }

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You don't quite seem to understand the idea of helping someone along the way instead of providing the complete solution. If I were an admin I would ban you for trolling.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    Code:
    #include<stdio.h>
    
     int main(void)
     {
    
    ...
    
    
     }
    Old saying....
    Give a man a fish and you've fed him for one day.
    Teach a man how to catch a fish and you've fed him for life.

    I agree with Claudiu.
    Nobody learns a thing when you just feed them the answer
    Moreover, it goes agains the educational nature of these forums.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by CommonTater View Post
    Old saying....
    Give a man a fish and you've fed him for one day.
    Teach a man how to catch a fish and you've fed him for life.

    I agree with Claudiu.
    Nobody learns a thing when you just feed them the answer
    Moreover, it goes agains the educational nature of these forums.
    Unfortunately, you have just stepped on a mine. A gazillion people will now jump on this thread saying that the purpose of these forums is not to be educational but to answer questions.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    Thanks for the help, although it seems that the only thing that kept my code from doing what i wanted it to do was the '\n' on the 9th line after the scanf function

    Kind regards
    Ryan

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    Unfortunately, you have just stepped on a mine. A gazillion people will now jump on this thread saying that the purpose of these forums is not to be educational but to answer questions.
    So that's what that loud click was? LOL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string i/o with files
    By Furious5k in forum C++ Programming
    Replies: 6
    Last Post: 12-24-2008, 03:13 PM
  2. Help please: Using I/O to Send Compiled Files
    By Hatchet in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2007, 02:39 PM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. Using .txt files (understading lines)
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 11-16-2004, 08:15 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM