Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 fstream problem
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: Thu Jan 06, 2005 4:44 pm   Post subject: fstream problem

ok so i have this function thats suppose to write into a file, now i get an error and it doesnt work somewhy

code:

void write ()
{
fstream fin;
fin.open("files/titles.txt",ios::out|ios::app);
char* data;
cout<<"Enter the data you want to insert inside the file\n";
cin>>data;

fin.write(data,int);

fin.close();

}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jan 06, 2005 5:53 pm   Post subject: (No subject)


  • No space between the functiona name and parentheses.

    code:
    void write()
    {

    }

  • Don't use "fstream". You're outputting to this stream, so use the ofstream class instead. Also, this is an output file, but you use something that's commonly seen as "file in". Bad programmer!

    code:
    ofstream output_file;

  • Why separately call openimmediately after the declaration? Just let the constructor do the work for you.

    code:
    ofstream output_file("files/titles.txt",ios::out|ios::app);

  • You're using a char array? That's C madness. This is C++ and we have proper strings.

    code:
    std::string data;

  • You're using cout without a namespace qualifier and \n instead of std::endl. Madness.

    code:
    std::cout << "Enter the data you want to insert inside the file" << std::endl;

  • Again, using cin without a qualifier...

    code:
    std::cin >> data;

  • Since we're now using strings that know their own length we don't have to use some crazy write function.

    code:
    output_file << data;

  • The close function is ok.
  • One last thing: you declare and initialize the output file before you actually use it in your function. C+ doesn't force you to do this.


code:
void write()
{
   std::string data;
   std::cout << "Enter the data you want to insert inside the file" << std::endl;
   std::cin >> data;

   std::ofstream output_file("files/titles.txt",ios::out|ios::app);
   output_file << data;
   output_file.close();
}
JR




PostPosted: Thu Jan 06, 2005 9:33 pm   Post subject: (No subject)

ok i tried to do the input from the file first i got something like

there are no errors, but it just doest output anything. i dont know why the file is in the right folder..,
code:

void read()
{
string data;

   ifstream fin("files/titles.txt",ios::in|ios::beg);

while(!fin.eof())
{
    fin >> data;
    cout<<endl;
}
   
while (!fin.eof())
{
    cout<<data;
}


   fin.close();

system("PAUSE"); //this is only to check if theres something inside
//console window gonna be erased later on.

}
wtd




PostPosted: Thu Jan 06, 2005 9:36 pm   Post subject: (No subject)

With all due respect, it kinda looks like you're just wildly guessing and copying and pasting. Are you working from a tutorial?
JR




PostPosted: Thu Jan 06, 2005 10:54 pm   Post subject: (No subject)

not really, but i read how to do the input/output.
wtd




PostPosted: Thu Jan 06, 2005 11:33 pm   Post subject: (No subject)

I would suggest you use a std::stringstream or std::vector object in reading in the file.

code:
#include <vector>
#include <string>
#include <iostream>
#include <fstream>


std::vector<std::string> read_lines(std::ifstream& in)
{
   std::vector<std::string> lines;
   std::string temp;

   while (!in.eof())
   {
      std::getline(in, temp, '\n');
      lines.push_back(temp);
   }

   return lines;
}

int main()
{
   std::ifstream in("io.cpp");
   std::vector<std::string> lines(read_lines(in));
   in.close();

   for (std::vector<std::string>::iterator i(lines.begin()); i != lines.end(); ++i)
   {
          std::cout << *i << std::endl;
   }

   return 0;
}


code:
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

std::string read_lines(std::ifstream& in)
{
   std::ostringstream ss;
   std::string temp;

   while (!in.eof())
   {
      std::getline(in, temp, '\n');
      ss << temp << std::endl;
   }

   return ss.str();
}

int main()
{
   std::ifstream in("io.cpp");
   std::string lines(read_lines(in));
   in.close();

   std::cout << lines << std::endl;

   return 0;
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: