LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2005, 06:57 PM   #1
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83

Rep: Reputation: 15
Question Using c++ to write to a text file. Im having problem with strings


Sorry this is a n00b question...


Ive hilighted where im getting a compiler error. (using Mucrosoft Visual .NET)


#include "stdafx.h"
#using <mscorlib.dll>
#include <iostream>
#include <string>
using namespace std;
using namespace System;
using namespace System::IO;

int _tmain()
{
string name, pass;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your Password: ";
cin >> pass;




try
{
// Create a FileStream
FileStream* fs = new FileStream(S"userinfo.txt", FileMode::Create);

// Create a StreamWriter
StreamWriter* sw = new StreamWriter(fs);

// Write some text
Quote:
sw->WriteLine(name);
sw->WriteLine(password);

// Close up the file
sw->Flush();
sw->Close();
}
catch(System::Exception* pe)
{
Console::WriteLine(pe->ToString());
}

return 0;
 
Old 02-02-2005, 07:45 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
Care to mention the error (number/description) as well?
Would really help looking for a solution ...


Cheers,
Tink
 
Old 02-02-2005, 08:04 PM   #3
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83

Original Poster
Rep: Reputation: 15
:-P meant to post those.
Quote:


Compiling...
CppWriter.cpp
CppWriter.cpp(29) : error C2664: 'void System::IO::TextWriter::WriteLine(System::Object __gc *)' : cannot convert parameter 1 from 'std::string' to 'System::Object __gc *'
Conversion from a built-in type or a value type to 'System::Object __gc*' requires boxing
CppWriter.cpp(30) : error C2065: 'password' : undeclared identifier
 
Old 02-02-2005, 11:02 PM   #4
SomeSleep
LQ Newbie
 
Registered: Feb 2005
Posts: 1

Rep: Reputation: 0
fixed it.

#using <mscorlib.dll>
#include <iostream>
#include <string>
using namespace System;
using namespace System::IO;
using namespace System::Text;




using namespace std;


int main()
{

char name[80], pass[80];
cout << "Enter your name: ";
cin >> name;
cout << "Enter your Password: ";
cin >> pass;




try
{
// Create a FileStream
FileStream* fs = new FileStream(S"c:\\Variables.txt", FileMode::Append, FileAccess::Write, FileShare::Write);
fs->Close();
StreamWriter* sw = new StreamWriter(S"c:\\Variables.txt", true, Encoding::ASCII);


String* NextLine=name;
sw->Write(NextLine);

String* spaceLine="\r\n";
sw->Write(spaceLine);

String* AfterLine=pass;
sw->Write(AfterLine);
sw->Close();









}
catch(System::Exception* pe)
{
Console::WriteLine(pe->ToString());
// cin there to pause the console on an error
cin >>name;
}

return 0;


}
 
Old 02-03-2005, 09:17 PM   #5
swatward
Member
 
Registered: Jan 2005
Distribution: slackware
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks, the code is better, it also was a problem with the .net. I needed to open a .net file, so it was mostly a compiler error.


Thanks a lot.
P.S.
also, would there be a way if there was allready a variables.txt in the c drive, say the program was run more than once, if it could just make the file name variables1.txt or variables2.txt? If its to compilcatred, dont even worry about it.

Last edited by swatward; 02-03-2005 at 09:36 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Is it possible to block text strings with IP tables? abefroman Linux - Security 27 06-29-2005 05:36 PM
Linux shell command for makefile.in to create a text file and write to it alix123 Programming 8 01-07-2005 08:18 AM
Formatted Strings in Script File mhjones Programming 1 06-07-2004 10:54 AM
php script can not write text file lemotion Linux - Newbie 5 04-20-2004 10:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration