Be a Supporter!

C++ fstream newbie problems

  • 288 Views
  • 5 Replies
New Topic Respond to this Topic
Halosheep
Halosheep
  • Member since: Apr. 9, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
C++ fstream newbie problems 2010-12-18 16:49:29 Reply

Just started to learn C++ a couple days ago, the basics weren't too difficult as I'm familiar with languages based on/influenced by it, i.e. Java and ActionScript, but I've run into problems with some File I/O practice programming.

I'm using CodeBlocks for Mac OS, and I'm writing a console application. Since just opening a file seemed a bit trivial, I wanted to try and see if I could open/create a file with the name the user entered and do stuff such as add user entered text into the file. Doesn't seem to hard, but it seems that my program can never locate the text file I made for it to open, called "example.txt". The problem can't be with file location or something because I made a copy of the file and put it in every folder in the project, so every level of the folder containing the project has a copy of the file.

After this happened, I tried just opening a file and printing out what it contained or adding stuff to it. Neither ever found the file or modified it in any way.

I don't know what to do, and I've checked google and whatnot, and got nothing. Any help would be greatly appreciated.

This is my code so far:

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main()
{
    char fileName[11];
    cout << "Please enter the name of the fle you wish to open:";
    cin.getline(fileName, strlen(fileName), '\n');

    ifstream fileImport (fileName);

    if(fileImport.is_open() == true)
    {
        char inputString[50];
        fileImport >> inputString;
        cout << "The file currently reads:\n";
        cout << inputString;
        cin.ignore();

        ofstream fileOutput (fileName, ios::trunc);
        char newOutput[50];
        cout << "Enter the new text for this file:\n";
        cin.getline(newOutput, 50, '\n');
        fileOutput << newOutput;
        cout << "The file now reads:\n";
        cout << newOutput << "\n";
        cin.ignore();
    }
    else
    {
        cout << "The file could not be read.\nExiting program on keystroke.";
        cin.ignore();
    }
}

"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes

mjfgates
mjfgates
  • Member since: Dec. 12, 2010
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to C++ fstream newbie problems 2010-12-18 18:06:03 Reply

It's worth noting that I hate streams, so have relatively little experience with them, but in a recent project I used

ifstream ifstr(ofn.lpstrFile);

if (!ifstr.bad() && !ifstr.eof())
{

to see if there was data to read, instead of is_open(). Iirc it WAS precisely because is_open() didn't work for me either.

Halosheep
Halosheep
  • Member since: Apr. 9, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to C++ fstream newbie problems 2010-12-19 07:29:29 Reply

Well it's not really that is_open() doesn't work, it's that the program can never find the file specified, and doesn't seem to automatically create one otherwise.

Could it be a problem with what I'm #Including, as I noticed before there seems to be separate libraries for things, such as there being two math libraries, cmath and math.h, and maybe I'm using the wrong fstream or string one?


"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes

kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to C++ fstream newbie problems 2010-12-19 07:37:44 Reply

cin.getline(fileName, strlen(fileName), '\n');

My bet is this is your problem, you're trying to find the length of a uninitialized string
Depending on your compiler and luck that strlen will either
A) evaluate to 0
B) give you a segfault and close the program.
C) evaluate to less than you need

try changing it to 11

Halosheep
Halosheep
  • Member since: Apr. 9, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to C++ fstream newbie problems 2010-12-19 08:01:37 Reply

Brilliant suggestion, and it probably fixed another problem, but it still doesn't find/create the file. I've also noticed that the open() and close() commands apparently don't exist.


"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes

Halosheep
Halosheep
  • Member since: Apr. 9, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to C++ fstream newbie problems 2010-12-20 09:56:35 Reply

Well, if no one knows how to fix this, then is there a workaround for opening/manipulating files, cus opening/manipulating files seems essential for doing nearly anything complicated.


"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes