I want to open a text file in a c program which is on desktop.How can I print the texts written in the file on screen.Please help


Regards
Prashant

Recommended Answers

All 10 Replies

Simple file io. First hit on google - http://www.cprogramming.com/tutorial/cfileio.html. All you need to do is point the file pointer to your desktop. You can hard code that if you want. Then open the file for reading (see link), and print its contents to screen.

i tried to create a simple test program bu it could not print the contents of the file myfile.txt.
please help!

#include<stdio.h>
int main()
{
    FILE *fp;
    fp=fopen("C:\\Documents and Settings\\Administrator\\Desktop\\myfile.txt", "w");
    fprintf(fp, "Testing...\n");
    getch();
}

Regards
Prashant

Do you want to print the contents to your screen or to the file? The way you opened the file there was for writing, that's what the w stands for. I was under the impression that you wanted to read the file. Use r for that. Look into fgets, or fscanf (if memory serves). http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html

I want to print the contents on screen and I also tried with "r" instead of "w" but it is not working!!

#include<stdio.h>
#include<conio.h>
int main()
{
    FILE *fp;
    fp=fopen("C:\\Documents and Settings\\Administrator\\Desktop\\myfile.txt", "r");
    fprintf(fp, "Testing...\n");
    getch();
}

It would be far more useful if you experimented with the code tags as opposed to the colour tags.

can u help me what I am doing wrong in above program??

can u help me what I am doing wrong in above program??

Yes, read the link you were given twice. It tells you how to do what you want. If not, ask a specific question.

Yes, read the link you were given twice. It tells you how to do what you want. If not, ask a specific question.

my question is should I give the full path name as C :\\Documents and Settings\\Administrator\\Desktop\\myfile.txt in the as fopen("C:\\Documents and Settings\\Administrator\\Desktop\\myfile.txt", "r") function or should I give only fopen ("myfile.txt" , "r") in order to open a file from desktop and display its content on screen.

my question is should I give the full path name as C :\\Documents and Settings\\Administrator\\Desktop\\myfile.txt in the as fopen("C:\\Documents and Settings\\Administrator\\Desktop\\myfile.txt", "r") function or should I give only fopen ("myfile.txt" , "r") in order to open a file from desktop and display its content on screen.

Then that's what you should ask.

Use the full path...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.