Hey!
I have a problen.

I'm a user of Turbo C++ yes, it's outdated. I know. LOL
SO anyway, i'm supposed to create a program that reads a text document when ran.

so I already have the text document named test.txt located on my desktop.
but when i try to run the program it doesn't display anything. I don't know why.
I've looked everywhere. But I couldn't figure out the problem.

So can you please tell me what's wrong with my code?:

    #include<stdio.h>
    #include<conio.h>
    FILE *myfile;
    main();
    {
        clrscr();
        myfile=fopen("C:\Documents and Settings\Migs\Desktop\test.txt","r");
        getch();
    }

Is there a mistake?

Recommended Answers

All 4 Replies

When you get the file from the desktop, is the file to be printed to the screen, to the printer or sent somewhere else? What line in your code shows this?

Hope this helps. Note that I didn't write it in Turbo C++, you'll have to make the right adjustments. Also, you might change the char file[256] line with char* file="C:\Documents and Settings\Migs\Dextop\test.txt" so that it will open the file directly from your dextop.
I took the liberty to make some nots along the code.

#include<stdio.h>

int main(){
    FILE *myfile;
    char file[256], text[256]; //chars which will contain the file's name and the file's buffer.
    printf("Insert filename: ");
    scanf("%255s", file);
    myfile=fopen(file,"r"); //opening file in reading mode.
    if (myfile==NULL)
        perror ("Invalid file.\n"); // if the file doesn't exist, if will print an error message.
    else
        while (!feof(myfile)) // while myfile != from end of file it will go through this loop.
            if (fgets(text, 255, myfile)!=NULL) // gets each line from the text file, if the line is not empty
                printf("%s", text); //prints the line
        fclose(myfile); //closes the file
    getchar();
    return (0);
}

And here's a C++ version of your program:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(){
    string file;
    cout<<"File name: ";
    cin>>file;
    ifstream fin (file.c_str(), ios::in);
    if (fin.good()){
        string line;
        while(getline(fin, line))
            cout<<line<<endl;
    }
    else cerr<<"Invalid file.\n";
    return (0);
}

If Turbo C is like all other Cs in the world then this line is wrong:

myfile=fopen("C:\Documents and Settings\Migs\Desktop\test.txt","r");

\ on it's own is nothing. You need to say \\ . In your case

myfile=fopen("C:\\Documents and Settings\\Migs\\Desktop\\test.txt","r");

\ means "special character follows" and then follow it with another backslash

A bit confusing I know.

owenransen, you are completely correct. The reason is the \ character defines the next character as an 'escaped' character.
\n = new line
\f = form feed
\t = tab
etc.

So, myfile=fopen("C:\Documents and Settings\Migs\Desktop\test.txt","r"); contains 2 escaped D's, 1 escaped M, and 1 escaped T (tab). That does not make a valid 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.