![]() |
Problem opening files
Hello,
I am trying to open a file that the user requests. I have figured out how to open a file that is hardcoded into the program, however like I said I can not seem to get it to work when I want it to open a file that the user requests through something like a "cin" statement. I have searched the internet for the answer for quite a while now and heeded no good results. I have found multiply different methods but none worked. EDIT: I am working with a Win32 console application Thank you, in advance. |
did you make sure that you have double slashes (escape) instead of an single slash:
for example if the user enters "c:\myfile.ext" you need to convert that to "c:\\myfile.ext". |
Quote:
one piece of code I found that didn't work: :
//global:
myFile = fopen("data.txt", "w+b"); |
Where is the namespace std after cin? You have to use std::cin if you don't directly state that you are 'using namespace std'. And also, where is your include statement, cin is located in the iostream header!
:
#include <iostream> |
Please read about how to post a good question. It's stuck right at the top of the forum.
That said, are you testing the result of the fopen instruction to see if it failed, and if so, why? You can't open a file that doesn't exist. There are a number of other reasons. Did you test to see if cin worked? Your code doesn't show that. Information is key. For you, and for your potential respondents. Check for errors. See what they mean. Use output statements to see what various values are. Use a debugger. SHARE that information, when you post, and include enough code so that we can see what you're actually doing. |
full code:
:
#include <iostream>(I edited the code slightly) I managed to figure out the answer to my first question. I noticed that this now will write the number and than load it fine. however, I need to figure out how to tell it to save a string. Also, I noticed that what it saves in the text file does not appear to be what it's loading. text file contains simply(yet it manages to load it right): Is it suppossed to save like that? |
That's not clear, either, but oh well. You're doing things the hard way, too, but let's also ignore that for the moment. If I take the bare bones of your code and clean it up a mere tad, so that it looks like this,
:
#include <iostream>Quote:
|
Well, thank you for your time. By playing with your code a little I managed to solve all my problems.
Thank you again. |
You might want to consider some alternate methods.
SOME WAYS TO OPEN AND USE A FILE Methods for opening files for reading and writing might be classified as C++, C stream, and C low-level methods. There are numerous variations, particularly with C++ streams; this post can in no way be considered exhaustive. CHECK YOUR DOCUMENTATION. A NOTE ABOUT MODE Unix/Linux have only one mode for file operations: binary. Windows has a text mode which is, unfortunately, the default. In text mode, content beyond what you supply is written to the file; it is stripped when you read the file. This makes the use of random access (seek, tell, etc.), problematic. Even worse, a 'soft' EOF, a special character, is written. This indicator is NOT MOVED if you do an append, so appended data is not reachable unless you reset the EOF indication. Consequently, I recommend always using binary mode. With 'fopen,' you can specify a read or write or other mode as "rb", "wb", etc. Unix/Linux will accept and ignore the 'b', so you can use it portably. You can also set the default mode in Windows with the global variable, _fmode. C++ USING FSTREAM VC++ 6.0, Windows XP Include iostream, fstream, and string for the following examples. FSTREAM OPENING FOR WRITE :
OPENING FOR READ :
A SEEK EXAMPLE :
Example output: Quote:
Include stdio.h for this method. OPENING FOR WRITE :
OPENING FOR INPUT :
Example output: Quote:
As far as I'm concerned, low level file operations are only good for reading large amounts of binary data, and are not necessary for that. One is far better off using the higher level functions. VC++ 6.0, Windows XP Include stdio.h, io.h, fcntl.h, sys\stat.h, ctype.h, and string.h for the following example. OPENING FOR READ/WRITE :
WRITING :
SEEK AND READ OPERATION -- Far more complex than higher level methods :
Example output: Quote:
|
| All times are GMT -5. The time now is 1:40 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC