Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 12-21-2006, 11:11 AM   #1
Baseband Member
 
Dnsgm's Avatar
 
Join Date: Jan 2006
Posts: 61
Default whats wrong with this?

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;
}
__________________

Dnsgm is offline   Reply With Quote
Old 12-21-2006, 01:01 PM   #2
Fully Optimized
 
UK31337's Avatar
 
Join Date: Feb 2005
Posts: 2,776
Default Re: whats wrong with this?

I've found a couple of problems and managed to fix it.

Code:
  
#include <iostream> #include <fstream> #include <string> #include <cstdio> // This should be <cstdio> using namespace std; int main () { string line; string copy; ifstream myfile ("C:\\Documents and Settings\\Rpg\\Desktop\\C++\\example.txt",ios::in); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; // strcpy(copy,line); -> you don't need this cout<< "\n" << copy; system("pause"); return 0; }
That works perfectly for me.
__________________

__________________
Master of common sense. If you don't like it, stop reading.
UK31337 is offline   Reply With Quote
Old 12-31-2006, 04:28 PM   #3
Baseband Member
 
Dnsgm's Avatar
 
Join Date: Jan 2006
Posts: 61
Default Re: whats wrong with this?

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;
}
Dnsgm is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:12 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0