English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi,

i have a few questions regarding how to read text files.

i'm asked to write a source code for that asks the user for a filename and print the contents of that filename that the user inputed (if it exists. if not the compiler must print that it doesn't exists). So how can i check if the filename does exists? Do i store the inputed filename into an array(if so what array size)?

can u pls give me a step by step proces on how to do it. btw we're only allowed to use stdio.h .

thanks.

2007-03-10 20:56:54 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

Well lets first clear up somethings. A filename is just the name of a file. A file contains information in this case some text. So I might have a file called MyInfo.txt. And say it contains:
This is a line.

Then the filename is MyInfo.txt and the code they want you to right should open up MyInfo.txt and read out "This is a line." and print it.

It should check if MyInfo.txt exists and if it doesn't print an error message.

This can be done by using the fopen() function, which will return an error if the file doesn't exist, (and you can print your error message if this happens), and if it does exist you will get a file pointer back, you use that file pointer with a fgets() to read the contains of the file, and printf() to print it out, and you use fclose() to close the file when you are finished. You put the fgets() and printf() in a loop that gets each line and ends when it gets to the end of the file contents.

2007-03-10 22:09:32 · answer #1 · answered by Bulk O 5 · 1 0

this can be solved fairly easily once you get used to the fopen function.
fopen is a function in stdio.h and it takes two arguments:
- the path of the file that you want to open
- the mode you want to access the file in(in this case you'll use r surrounded by quotes)

fopen will return a NULL value if there was an error, in this case if the file doesn't exist. fopen does set the errno value so you can use that to distiguish what the problem was but that is ussually used in more advanced programs

so your code will look like this
main(int argc,char * argv[])
{
File *fp = fopen(argv[1],'r');/*if this spits an error copy argv[1] into a char array then use that as the parameter*/

if(*fp = NULL)
printf("The file does not exist.");

/*finish the rest of the program here*/
}

2007-03-16 16:55:50 · answer #2 · answered by Mike 2 · 0 0

#include

/* Declare the file pointer and filename variable*/
FILE *file;
char filename[15];

main()
{
/* Declare a temporary string to store your file content */
char *text;

clrscr();
/* Ask the user to input the filename */
printf("\n\t\tEnter Filename: ");
scanf("%[^\n]", filename);
fflush(stdin);

printf("Filename is %s.\n", filename);

/* Try to open the file */
if((file = fopen(filename, "r")) != NULL)
{ /* If the file exist, display all the contents on the screen until end of file */
while(!feof(file))
{
fread(text, 32, 1, file);
printf("%s", text);
}
/* Make sure you close the file after end of file */
fclose(file);
} else {
/*If file doesn't exist, display the Message */
printf("\n\t\tFILE %s DOES NOT EXIST!!", filename);
}
getch();
}


2007-03-18 18:12:29 · answer #3 · answered by Liviawarty J 2 · 0 0

well first you need to read open your file for reading in c++ this can be done using fstream, not sure about c in any case you wont be able to create an array at runtime since you dont know how big the file is

so your gonna have to allocate space for it in the heap using malloc and then store values using pointers

its kina complicated but hope this gives you an idea :)

2007-03-10 22:05:07 · answer #4 · answered by dragongml 3 · 0 0

It quite would not artwork that way. Backup is an extremely complicated ingredient, and there is not any hassle-free thank you to do it perfect. it rather is merely no longer a good thank you to start gaining expertise of C++. in case you like a backup application, there are various excellent ones. in case you opt for to income C++, finding at complicated code would not quite enable you to. (you won't attempt to income a thank you to play piano by way of listening to a grasp play a complicated piece, might you?)

2016-10-01 22:32:57 · answer #5 · answered by ? 4 · 0 0

fedest.com, questions and answers