Hi! I want to write a programme that asks the user to enter the name of a .txt file, and then reads it.
So, here's my code:
Code:
#include <stdio.h>
#include <string.h>
int open_file(char a[30], int *b) //This function asks for the name of the file and opens it//
{
gets(a);
strcat(a, ".txt");
*b = fopen(a, "r");
}
main()
{
FILE *archivo;
char file_name[30], text[30];
open_file(file_name, &archivo);
while (!feof(archivo))//This part reads the file//
{
fgets(text, 30, archivo);
printf("%s", text);
}
getch();
}
This works fine. However, when I put the "read" part into a function it doesn't work!!
Code:
#include <stdio.h>
#include <string.h>
int open_file(char a[30], int *b)
{
gets(a);
strcat(a, ".txt");
*b = fopen(a, "r");
}
int read_file(char c[30], int *d)
{
while (!feof(*d))
{
fgets(c, 30, *d);
printf("%s", c);
}
getch();
}
main()
{
FILE *archivo;
char file_name [30], text[30];
open_file(file_name, &archivo);
read_file(text, &archivo);
}
Where's the problem? Can anybody help me?
Thanks!!
P.S: I use Dev-Cpp in Windows XP