whats wrong with this?

Dnsgm

Baseband Member
Messages
61
I'm trying to strcpy a string file a file to the program; whats wrong with this?

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio>
using namespace std;

int main () {
  string line;
  string copy;
  
  ifstream myfile ("C:\\Documents and Settings\\Rpg\\Desktop\\C++\\example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }
  
  else cout << "Unable to open file"; 
  
  strcpy(copy,line);
  
  cout<< "\n" << copy;
  
  system("pause");
  return 0;
}
 
I've found a couple of problems and managed to fix it.

Code:
[left]#include <iostream>
#include <fstream>
#include <string>
#include <[b][color=red]c[/color][/b]stdio> [b][color=red]// This should be <cstdio>[/color][/b]
using namespace std;

int main () {
  string line;
  string copy;
  
  ifstream myfile ("C:\\Documents and Settings\\Rpg\\Desktop\\C++\\example.txt",[color=red][b]ios::in[/b][/color]);
  if (myfile.is_open())
  {
	while (! myfile.eof() )
	{
	  getline (myfile,line);
	  cout << line << endl;
	}
	myfile.close();
  }
  
  else cout << "Unable to open file"; 
  
  [color=red][b]// strcpy(copy,line); -> you don't need this[/b][/color]
  
  cout<< "\n" << copy;
  
  system("pause");
  return 0;
}[/left]

That works perfectly for me.
 
thanks that worked out well =)

but now, there's something wrong with my if else statements; always goes with the first IF

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main () 
{
  
  int tables;
  int loop;
  string title;
  string line;
  
  ifstream yourfile ("C:\\Documents and Settings\\John\\Desktop\\example.txt",ios::in);
  if (yourfile.is_open())
  {
	while (! yourfile.eof() )
	{
	  getline (yourfile,line);
	}
	yourfile.close();
  }
  
  else cout << "Unable to open file"; 
  
  ofstream myfile;
  myfile.open ("C:\\Documents and Settings\\John\\Desktop\\webgen.html");
  
  myfile << "<html>\n";
  myfile << "<head>\n";
  
  cout <<   "What is the title of your website: ";
  cin  >>   title;
  
  myfile << "<title> " << title << "</title>\n";
  myfile << "</head>\n";
  myfile << "<body>\n";
  
  cout   << "How many seperate tables would you like: ";
  cin    >> tables;
  
  do
  {
         int border;
         int cellpadding;
         int position_type;
         int position;
         int position_left;
         int position_top;
         int bg_color;
         int cellspacing;
         int width;
         
         
         cout << "How thick would you like your border(0,1,2,...): ";
         cin  >> border;
         
         cout << "\nWhere would you like your table(auto=1, manual=0): ";
         cin  >> position_type;


         if (position_type = 1) 
         {
            cout << "\nTable position has been set to AUTO.\n";
         }
         
         if (position_type = 0)  
         {
              cout << "\nHow far wouild you like your table from the left: ";
              cin  >> position_left;
                           
              cout << "\n\n how far would you like your table from the top: ";
              cin  >> position_top;
         }

         
         
         loop = loop + 1;
  }
  while (loop < tables);

  
  myfile.close();
  system ("pause");
  return 0;
}
 
Back
Top Bottom