CANT WRITE TO A FILE

[b][red]This message was edited by mvbozkurt at 2003-3-31 21:36:36[/red][/b][hr]
Im having an output problem with my code here!!!
Please run and tell me what is wrong cause usually I dont use stl fstream objects for input and output.
-----------------------------------------------------------------------
ifndef WORD_H
#define WORD_H

#include
#include
#include
#include

#define TRUE 1
#define FALSE 0
#define BUFFER_SIZE 30
#define PATH_LENGTH 100

using namespace std;

class Word
{

public:
Word(string = NULL ,int = 0);
int operator==(const Word&);
const string Display_str (void);
const int Display_int (void);
void Times_inc (void);
private:
string word;
int times;
};

#endif

-----------------------------------------------------------------------
#include "Word.h"

Word::Word(string temp_word,int temp_times)
{
word = temp_word;
times = temp_times;

}

int Word::operator==(const Word& right)
{
if (right.word == this->word)
return TRUE;
else
return FALSE;
}

-----------------------------------------------------------------------


#include "Word.h"

typedef list LIST;

int main (void)
{
char_traits::char_type word_buffer[BUFFER_SIZE],filename[PATH_LENGTH];
string word_buffer_str;
int total_words = 0,total_letters = 0,total_sentences = 0;
float avg_words, avg_letters;
fstream file;
LIST word_list;
LIST::iterator i;
int status = FALSE,counter =0;

while(TRUE)
{
cout<<"

Enter the filename: ";
cin>>filename;
file.open(filename,ios_base::in);
if(file.is_open() == FALSE)
cout<<"
File not found";
else
{
cout<<"
File opened";
break;
}
}
while(!file.eof())
{
status = FALSE;
file.getline(&word_buffer[0],BUFFER_SIZE,' ');
word_buffer[0] = tolower(word_buffer[0]);
if ( strlen(word_buffer) > 1 || word_buffer[0] == 'a')
{
total_words++;
if( toupper(word_buffer[(strlen(word_buffer)-1)]) < 65 ||
toupper(word_buffer[(strlen(word_buffer)-1)]) > 90)
{
if(word_buffer[(strlen(word_buffer)-1)] == '.')
total_sentences++;
word_buffer[(strlen(word_buffer)-1)] = '';
}

else;
total_letters = strlen(word_buffer);
cout<<"
BUFFER "<<word_buffer;
Word* memtest = new Word (word_buffer,1);

if (memtest == NULL)
{
cout<<"

CAN'T ALLOCATE MEMORY";
return 0;
}
else
{
delete memtest;

}
word_buffer_str = word_buffer;

if(word_list.empty())
{
word_list.push_back(*new Word (word_buffer_str,1));
status = TRUE;
}
else
{
for(i=word_list.begin();i != word_list.end();i++)
{
if(word_buffer_str == i->Display_str())
{
i->Times_inc();
status = TRUE;
cout<<"++";
break;
}
else if ( word_buffer_str < i->Display_str() )
{
word_list.insert(i,*new Word (word_buffer,1));
status = TRUE;
cout<<"<<";
break;
}
else;
}
if (status == FALSE)
{
word_list.push_back(*new Word (word_buffer_str,1));
cout<<"F";
}
}
}
}
file.close();

while(TRUE)
{
cout<<"

Enter the output filename: ";
cin>>filename;
file.open(filename,ios_base::in);
if(file.is_open())
{
file.close();
cout<<endl<<filename<<" file exists in the destination folder.";
cout<<"
To overwrite 1
To append 2
To change filename 3";
cout<<"
Enter selection(1-3): ";
cin>>status;
switch(status)
{
case 1:
file.open(filename,ios_base::out | ios_base::trunc);
status = -222;
break;
case 2:
file.open(filename,ios_base::out | ios_base::app);
status = -222;
break;
default:
cout<<"
You made a wrong selection or wanted to change filename";
file.close();
break;
}
}
else
{
file.open(filename,ios_base::out | ios_base::trunc);
status = -222;
}
if (status == -222)
break;
}
cout<<"



"<<filename<<" file word count";
for (i=word_list.begin();i != word_list.end();i++)
{
file.write("aaa",20);
file<<endl<<i->Display_str();
file.put('a');
file<<"aAA";
cout<<endl<<i->Display_str()<<" is used "<<i->Display_int()<<" times";
}
file.close();
cin>>total_words;
return 0;
}

-----------------------------------------------------------------------
Thanks.

PS: Please run the code and see if it stores in specified file
And also dont write replies for newbies!!!!!
const string Word::Display_str(void)
{
return word;
}
const int Word::Display_int(void)
{
return times;
}

void Word::Times_inc (void)
{
this->times++;
}



Comments

  • you opened the file for input, not output.
    [code]
    while(TRUE)
    {
    cout<<"

    Enter the output filename: ";
    cin>>filename;
    file.open(filename,[red]ios_base::in)[/red];
    if(file.is_open())
    {
    file.close();
    cout<<endl<<filename<<" file exists in the
    [/code]
  • [BLUE]TO STOBER[/BLUE]
    [GREEN]OK BUDDY EITHER YOU HAVE NO IDEA ABOUT C++ OR YOU DONT KNOW HOW TO READ!!![/GREEN] DID YOU READ THE REST OF THE CODE BEFORE YOU SAY [red]YOU OPENED FILE FOR READING!!![/red] CAUSE IF YOU READ THE REST OF THE CODE YOU WILL SEE THE OPEN MODE....
    AND PLEASE IF YOU HAVE NO IDEA WHY DO YOU BOTHER REPLYING...
  • : [BLUE]TO STOBER[/BLUE]
    : [GREEN]OK BUDDY EITHER YOU HAVE NO IDEA ABOUT C++ OR YOU DONT KNOW HOW TO READ!!![/GREEN] DID YOU READ THE REST OF THE CODE BEFORE YOU SAY [red]YOU OPENED FILE FOR READING!!![/red] CAUSE IF YOU READ THE REST OF THE CODE YOU WILL SEE THE OPEN MODE....
    : AND PLEASE IF YOU HAVE NO IDEA WHY DO YOU BOTHER REPLYING...
    :
    [red]if you're going to be so nasty, then FU[/red]
  • : [BLUE]TO STOBER[/BLUE]
    : [GREEN]OK BUDDY EITHER YOU HAVE NO IDEA ABOUT C++ OR YOU DONT KNOW HOW TO READ!!![/GREEN] DID YOU READ THE REST OF THE CODE BEFORE YOU SAY [red]YOU OPENED FILE FOR READING!!![/red] CAUSE IF YOU READ THE REST OF THE CODE YOU WILL SEE THE OPEN MODE....
    : AND PLEASE IF YOU HAVE NO IDEA WHY DO YOU BOTHER REPLYING...
    :
    I have to say that I always find Stober's responses to be insightful and informative.

    From your coding style, I would say that you are either a student or a very poor C++ programmer. Your code is not commented, overly complicated, and poorly constructed.

    You should do yourself a favor and try asking constructive questions, rather than posting nasty responses to people that are trying to help you.

    I would offer to help you out, but I'm afraid that you would consider my solution too "newbie" for you to consider.
  • [b][red]This message was edited by mvbozkurt at 2003-4-1 21:17:57[/red][/b][hr]
    [b][red]This message was edited by mvbozkurt at 2003-4-1 21:9:24[/red][/b][hr]
    Ok buddy I didnt call myself the best programmer ever
    Here is the thing Im a student and maybe the worst programmer on the whole planet but anyway I dont tell people what's wrong with their code unless Im very sure of it.

    If you say that file is open for writing by reading this line[GREEN]
    out<<"

    Enter the output filename: ";
    cin>>filename;
    file.open(filename,ios_base::in);[/GREEN]
    you can easily see that I close the file 2 lines later to change the open mode [RED] and also as you might know (at least as a poor programmer I know) if you want check file existince for overwriting (for security reasons) the file is opened by read mode first [/RED] assume that you dont know that I can tell you if you read the next 5 lines
    [GREEN]
    file.close();
    cout<<endl<<filename<<" file exists in the destination folder.";
    cout<<"
    To overwrite 1
    To append 2
    To change filename 3";
    cout<<"
    Enter selection(1-3): ";
    cin>>status;
    switch(status)
    {
    case 1:
    file.open(filename,ios_base::out | ios_base::trunc);
    status = -222;
    break;
    case 2:
    file.open(filename,ios_base::out | ios_base::app);
    status = -222;
    break;
    default:
    cout<<"
    You made a wrong selection or wanted to change filename";
    file.close();
    break;
    }
    }
    else
    {
    file.open(filename,ios_base::out | ios_base::trunc);
    status = -222;
    [/GREEN] you will see the open modes.

    Thanks anyway
    PS: And I never comment an non working code (like it or not) also if a code is highly complicated instead of saying that I always look for the reason behind it[RED]belive me there is always a reason[/RED]




  • : [b][red]This message was edited by mvbozkurt at 2003-4-1 21:17:57[/red][/b][hr]
    : [b][red]This message was edited by mvbozkurt at 2003-4-1 21:9:24[/red][/b][hr]
    : Ok buddy I didnt call myself the best programmer ever
    : Here is the thing Im a student and maybe the worst programmer on the whole planet but anyway I dont tell people what's wrong with their code unless Im very sure of it.
    :
    : If you say that file is open for writing by reading this line[GREEN]
    : out<<"

    Enter the output filename: ";
    : cin>>filename;
    : file.open(filename,ios_base::in);[/GREEN]
    : you can easily see that I close the file 2 lines later to change the open mode [RED] and also as you might know (at least as a poor programmer I know) if you want check file existince for overwriting (for security reasons) the file is opened by read mode first [/RED] assume that you dont know that I can tell you if you read the next 5 lines
    : [GREEN]
    : file.close();
    : cout<<endl<<filename<<" file exists in the destination folder.";
    : cout<<"
    To overwrite 1
    To append 2
    To change filename 3";
    : cout<<"
    Enter selection(1-3): ";
    : cin>>status;
    : switch(status)
    : {
    : case 1:
    : file.open(filename,ios_base::out | ios_base::trunc);
    : status = -222;
    : break;
    : case 2:
    : file.open(filename,ios_base::out | ios_base::app);
    : status = -222;
    : break;
    : default:
    : cout<<"
    You made a wrong selection or wanted to change filename";
    : file.close();
    : break;
    : }
    : }
    : else
    : {
    : file.open(filename,ios_base::out | ios_base::trunc);
    : status = -222;
    : [/GREEN] you will see the open modes.
    :
    : Thanks anyway
    : PS: And I never comment an non working code (like it or not) also if a code is highly complicated instead of saying that I always look for the reason behind it[RED]belive me there is always a reason[/RED]
    :
    :
    :
    :
    :
    I didn't say that your code was highly complicated, I said that it was OVERLY complicated, which means that there are much easier ways to code what you are trying to achieve.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories