GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Jul-2006, 07:04
Tomb332's Avatar
Tomb332 Tomb332 is offline
Junior Member
 
Join Date: Jul 2006
Location: Everywhere and yet nowhere at all
Posts: 47
Tomb332 is on a distinguished road
Post

File opening problem


Simple question how do you open a file in a folder eg files\help\help1.txt
because if you put that as the address to open a ofstream file

CPP / C++ / C Code:
ofstream fout("files\help\help1.txt");

it thinks it is a file called fileshelphelp1.txt

PS:What do you think of the avatar, i drew parts myself
__________________
Forgive my english
(I am english i just can't spell)
PS:What do you think of the avatar, i drew parts myself
  #2  
Old 29-Jul-2006, 07:33
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: File opening problem


Quote:
Originally Posted by Tomb332
Simple question how do you open a file in a folder eg files\help\help1.txt
because if you put that as the address to open a ofstream file

CPP / C++ / C Code:
ofstream fout("files\help\help1.txt");

it thinks it is a file called fileshelphelp1.txt

PS:What do you think of the avatar, i drew parts myself

I think you should use \\ instead of \
The Avatar looks very nice

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #3  
Old 29-Jul-2006, 11:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: File opening problem


Quote:
Originally Posted by kobi_hikri
I think you should use \\ instead of \
Yes, Kobi is correct. The \ character always escapes the next character, or turns it into another predefined special character, so with "files\help\help1.txt" you have 2 '\h' characters, but \h isn't defined so the \ is thrown out.

See this for a list of special chars.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #4  
Old 30-Jul-2006, 03:31
Tomb332's Avatar
Tomb332 Tomb332 is offline
Junior Member
 
Join Date: Jul 2006
Location: Everywhere and yet nowhere at all
Posts: 47
Tomb332 is on a distinguished road

Re: File opening problem


It doesn't work the program gets confused and can't write or read the file
__________________
Forgive my english
(I am english i just can't spell)
PS:What do you think of the avatar, i drew parts myself
  #5  
Old 30-Jul-2006, 09:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: File opening problem


Quote:
Originally Posted by Tomb332
It doesn't work the program gets confused and can't write or read the file

Post the entire code. Tell us what compiler and operating system you are using. If it is Windows, tell us what version (Windows98, Windows XP, or whatever). If you are running from inside a Graphical Interface (Visual C++ GUI, or dev-c++ or whatever) tell us that also. If you are running from a command line (window with a command prompt) tell us that also.

Regards,

Dave
  #6  
Old 30-Jul-2006, 13:35
Tomb332's Avatar
Tomb332 Tomb332 is offline
Junior Member
 
Join Date: Jul 2006
Location: Everywhere and yet nowhere at all
Posts: 47
Tomb332 is on a distinguished road

Re: File opening problem


i can't post the whole program because this is one part of a huge program (so far 6 header files 2 source files)

this is the code it is a help display function.
CPP / C++ / C Code:
#include <fstream>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ofstream;
using std::ifstream;


void printHelp(){
    char mainBuffer[122];
    char noKnowBuffer[83];
    char typesBuffer[427];
    char searchBuffer[238];
    char quantifyBuffer[162];
    int  inputData;
    bool helpActive = true;

    ifstream fin("files\\help\\helpMain.txt");
    fin.get(mainBuffer, 111, '0');
    fin.close();

    ifstream fin2("files\\help\\helpNoKnow.txt");
    fin2.get(noKnowBuffer, 72, '0');
    fin2.close();

    ifstream fin3("files\\help\\helpTypes.txt");
    fin3.get(typesBuffer, 416, '0');
    fin3.close();

    ifstream fin4("files\\help\\helpSearch.txt");
    fin4.get(searchBuffer, 227, '0');
    fin4.close();

    ifstream fin5("files\\help\\helpQuantify.txt");
    fin5.get(quantifyBuffer, 151, '0');
    fin5.close();

    cout << mainBuffer << "\n";

    while(helpActive){
        cout << "What area of the program do you want info on?" << "\n";
        cout << "(1)Types of item (2)New (3)Search (4)Quantify (5)Exit Help" << "\n";
        cin >> inputData;

        switch(inputData){

            case 1:
                cout << typesBuffer << "\n";
            break;

            case 2:
                cout << noKnowBuffer << "\n";
            break;

            case 3:
                cout << searchBuffer << "\n";
            break;

            case 4:
                cout << quantifyBuffer << "\n";
            break;

            case 5:
                helpActive = false;
            break;

            default:
                cout << "You must enter a valid number \n";
        }
    }
}

Output:
Should be a function that displays a recuring menu which displays the contents of different files depending on the selection.

Info:
Compiler: Visual C++.Net Standard (2003 version).
OS: Windows XP Home edition.
App Type: Win32 Console app.
Run Type: Standard Click on the icon.

PS I know the buffers are wasteful but i haven't got around to changing it yet.
__________________
Forgive my english
(I am english i just can't spell)
PS:What do you think of the avatar, i drew parts myself
  #7  
Old 30-Jul-2006, 14:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: File opening problem


Quote:
Originally Posted by Tomb332
i can't post the whole program because this is one part of a huge program (so far 6 header files 2 source files)
OK, I understand. And it's really better for you to post the smallest amount of code that we can use to test on our systems instead of the whole huge stuff.
Quote:
Originally Posted by Tomb332
this is the code it is a help display function.

But you should give enough code that we can compile and test. In fact, by reducing the code to a smaller amount that still displays the error, it might be quicker and easier for you to test and debug. (And faster than posting a request for help and waiting for a helpful response, I claim. It's always OK to ask, but...)

A couple of program notes:

First of all: When you open files, I think you should always test to see if it was successful.

Secondly: When you get data input (from files or from user input on a console, or whatever...), I think you should always test to see if the conversion was successful.

CPP / C++ / C Code:
#include <fstream>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char *inname = "files\\help\\helpMain.txt";
    char mainBuffer[122];
    ifstream fin(inname);
    if (fin.fail()) {
        cout << "There was a problem opening "
             << inname
             << " for reading." 
             << endl;
    }
    else {
        cout << "Opened " << inname << " for reading." << endl;
        fin.get(mainBuffer, 111, '0');
        if (fin.fail()) {
            cout << "There was a problem reading from "
                 << inname
                 << endl;
        }
        else {
            cout << "Here's what was read from " << inname << endl;
            cout << mainBuffer << endl;
            fin.close();
        }
    }
    cout << "Press 'Enter' to close the window: " << flush;
    getchar();

    return 0;
}

And my output:
Code:
Opened files\help\helpMain.txt for reading. Here's what was read from files\help\helpMain.txt This is a test. There are two lines, And this is the end. Press 'Enter' to close the window:

Get this to run correctly first, then fix up your large program.

Notes: You indicated that it is a Windows console application and you run the program by "standard click on the icon". I don't know exactly what this means to you, but here's what I did:

I compiled the program, then with Windows Explorer, I navigated to the place where the ".exe" file was located. I double-clicked on the ".exe" thing and the program ran. Another way: I right-clicked on the .exe file in the Windows Exlplorer pane, dragged it to the Desktop, and when I released the mouse button, I selected "Create Shortcuts here". Now, I can double-click on the desktop icon to get it to run.

Note that successful execution depends on the folder named "files" being under the folder where the .exe file is, and a folder "help" must be under that, and the file named helpMain.txt is there.

(Normally, when I create command-line applications, I run them from a command line, not by clicking on the icon. That way, there's no question as to where the executable file is, and where the data files are, relative to it. I really prefer it that way, but that's just me.)

In other words, when you use the "click on the icon" method to launch a program the default behavior is that the files are relative to the directory where the executable is located.

If you right-click on the Desktop icon, you will see a window called "start in" and the path will be the path to your executable file. You can change this so that the files\help stuff can be anywhere you want. If you want to specify a particular path in your C file rather than a path relative to the location of the .exe file, then you can make the path absolute. For example if the help files are on the root of drive D:
CPP / C++ / C Code:
char *inname = "D:\\helpfiles\\help".

If you want to create an output file, use the same rules for locating the file (absolute path or path relative to executable).

Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Download files in c for windows operating system oozsakarya C Programming Language 5 20-Jun-2006 04:33
difference between "rb" mode file opening and "rt" mode file opening jaininaveen C Programming Language 3 15-Feb-2006 14:15
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:14.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.