473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

FileStream In VC++ 2005

abdoelmasry
104 100+
Hi
Im newer in VC++
i need help to write to file

what's wrong with this code:

Expand|Select|Wrap|Line Numbers
  1.    FileInfo^ newfile=gcnew FileInfo("abdo.txt");
  2.    FileStream^ nsfile=newfile->Create();
  3.    nsfile->Write('abdo',0,5);
  4.    nsfile->Close();
ThX
Apr 19 '07 #1
10 8965
sicarie
4,677 Expert Mod 4TB
Hi
Im newer in VC++
i need help to write to file

what's wrong with this code:

Expand|Select|Wrap|Line Numbers
  1.    FileInfo^ newfile=gcnew FileInfo("abdo.txt");
  2.    FileStream^ nsfile=newfile->Create();
  3.    nsfile->Write('abdo',0,5);
  4.    nsfile->Close();
ThX
abdoelmasry-

Please have a look at our Posting Guidelines, specifically the part about asking good questions. I have no idea what is wrong with that code, as I don't know what you're trying to do, what it's actually doing, any error messages that might be coming up, etc...

Please be a bit more specific (and thanks for using code tags!).
Apr 19 '07 #2
abdoelmasry
104 100+
Sorry sicarie
I Just Trying to Create File And Write Some Words on it
using Visual C++ 2005

Del Submit If U Need
Sorry for bad submit
Apr 19 '07 #3
sicarie
4,677 Expert Mod 4TB
Sorry sicarie
I Just Trying to Create File And Write Some Words on it
using Visual C++ 2005

Del Submit If U Need
Sorry for bad submit
It's not that big an issue that we're going to delete your post - we do want to help you, but I'm not sure what code you do and don't have aside of that, so I can't (and as this is your proejct probably wouldn't anyway) put it into a compiler and run it (that's your job).

Which is why I'm asking - are you getting an error message with that code? Is it compiling? Or can you just not write to the file? If you're getting an error, can you copy and paste it here?
Apr 19 '07 #4
abdoelmasry
104 100+
Hi sicarie

It's The first time for me to use visual c++ 2005
i need any code to create file and write & read
i have found FileStream Function
I make sample code While i tryed to compile i got this error:

error C2664: 'void System::IO::Stream::Write(cli::array<Type,dimensio n> ^,int,int)' : cannot convert parameter 1 from 'System::String ^' to 'cli::array<Type,dimension> ^'
with
[
Type=unsigned char,
dimension=1
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

This Code Worked God To write to file:

Expand|Select|Wrap|Line Numbers
  1. StreamWriter^ nfile=File::CreateText("abdo.txt");
  2. String^ nam="www.TheScripts.com";
  3. nfile->WriteLine(nam);
  4. nfile->Close();
i need code to read text file

I hope i could explain
Apr 20 '07 #5
abdoelmasry
104 100+
Hi sicarie

It's The first time for me to use visual c++ 2005
i need any code to create file and write & read
i have found FileStream Function
I make sample code

This Code Worked God To write to file:

Expand|Select|Wrap|Line Numbers
  1. StreamWriter^ nfile=File::CreateText("abdo.txt");
  2. String^ nam="www.TheScripts.com";
  3. nfile->WriteLine(nam);
  4. nfile->Close();

Im Trying To read file Using this code:

Expand|Select|Wrap|Line Numbers
  1. StreamReader^ nfile=File::OpenRead("abdo.txt");
  2. String^ nam=nfile->ReadLine();
  3. nfile->Close();
but it's not working
i got error:

Expand|Select|Wrap|Line Numbers
  1. error C2440: 'initializing' : cannot convert from 'System::IO::FileStream ^' to 'System::IO::StreamReader ^'
i need code to read text file

I hope i could explain
Apr 20 '07 #6
sicarie
4,677 Expert Mod 4TB
Hi sicarie

It's The first time for me to use visual c++ 2005
i need any code to create file and write & read
i have found FileStream Function
I make sample code

This Code Worked God To write to file:

Expand|Select|Wrap|Line Numbers
  1. StreamWriter^ nfile=File::CreateText("abdo.txt");
  2. String^ nam="www.TheScripts.com";
  3. nfile->WriteLine(nam);
  4. nfile->Close();

Im Trying To read file Using this code:

Expand|Select|Wrap|Line Numbers
  1. StreamReader^ nfile=File::OpenRead("abdo.txt");
  2. String^ nam=nfile->ReadLine();
  3. nfile->Close();
but it's not working
i got error:

Expand|Select|Wrap|Line Numbers
  1. error C2440: 'initializing' : cannot convert from 'System::IO::FileStream ^' to 'System::IO::StreamReader ^'
i need code to read text file

I hope i could explain
Hmm, is this C#?

If you are attempting to read and write to the file, my guess (for C++) is that the fstream is not declared for both reading and writing (in a single declaration). I believe it's similar in C#, but I'm not sure of the differences between the two.
Apr 20 '07 #7
vermarajeev
180 100+
Cosider this sample program

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. int main()
  5. {
  6.    ofstream ofs(" test.txt");
  7.    if( !ofs.is_opsn() )
  8.       return -1;
  9.  
  10.    while( ofs.end() )
  11.     {
  12.               //write data to file.
  13.     }
  14.    ofs.close();
  15.  
  16.    ifstream ifs( "test.txt" );
  17.    if( !ofs.is_open() )
  18.       return -1;
  19.  
  20.    while( ifs.eof() )
  21.    {
  22.        //read data from file;
  23.    }
  24.    ifs.close();
  25.    return 0;
  26. }
I have not tested this code and there may be some errors.

Thanks
Apr 21 '07 #8
Atli
5,058 Expert 4TB
Hi.

The objects you are using (FileStream) are a part of the .Net framework.
I do not know why you chose to use C++ but if you are using .Net classes your code will need the .Net runtime to run, in which case it would be much easier to use C#. It is much easyer to use.

The code posted by vermarajeev is native C++ code which is imo the only good reason to use C++, because then you do not need the .Net runtime to run the application.

The reason for your error however, is that you are trying to use the ReadLine() method on a FileStream object, but that method is a part of the StreamReader object so C++ gets confused and tries to convert it, which causes the error.

You would have to do something like this
Expand|Select|Wrap|Line Numbers
  1. // Create FileStream
  2. System::IO::FileStream^ fs = gcnew System::IO::FileStream(
  3.    "myfile.txt",
  4.    System::IO::FileMode::Open,
  5.    System::IO::FileAccess::Read
  6. );
  7.  
  8. // Create StreamReader
  9. System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(fs);
  10.  
  11. // Read file
  12. System::String^ text = sr->ReadToEnd();
  13.  
  14. // Close streams
  15. fs->Close();
  16. sr->Close();
  17.  
Apr 21 '07 #9
Atli
5,058 Expert 4TB
What I posted before was kind of the long way arround, this is perhaps easyer:
Expand|Select|Wrap|Line Numbers
  1. // Create StreamReader
  2. System::IO::StreamReader^ sr = 
  3.    gcnew System::IO::StreamReader(System::IO::File::OpenRead("myfile"));
  4.  
  5. // Read file
  6. System::String^ text = sr->ReadToEnd();
  7.  
  8. // Close streams
  9. sr->Close();
  10.  
Apr 21 '07 #10
abdoelmasry
104 100+
Thank U Great Man
I know some things in c++
but i wanted to learn c++ 2005

the code is working good
Apr 21 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Hosalli | last post by:
Can I save my work done in VC++ 2005 Express Edition so as to work in VC++ 6.0? If yes how do I do it? I need this as I've got to submit my assignments to my instructor who uses VC++ 6.0. ...
2
by: um | last post by:
When the POSIX pthreads library for w32 release 2-2-0 (http://sources.redhat.com/pthreads-win32/) is compiled with VC++6 then it compiles and passes all the benchmark tests in the subdirectory...
8
by: david_75 | last post by:
I wonder if the transistion from the project written in VC++ 6.0 to VC++ .NET requires a lot of code changes (if any) if I compile the project in native code (or unmanaged code) without using the...
9
by: Tim_Mac | last post by:
hi, i'm not sure if i have chosen the best approach, but it seemed quite good to me. i have a collection class, containing business objects. the collection class is static and remains in-memory...
7
by: Mihajlo Cvetanović | last post by:
Hi all, I've been trying to find some info on Visual C++ 2005 Standard on Microsoft's site, but wasn't able to find any. There's only VC++ 2005 Express Edition, and Visual Studio 2005 Standard,...
4
by: Lloyd Dupont | last post by:
I need to access big data in a readonly fashion. I was thinking to open a FileStream and seek and read as needed. I was wondering if it was worth doing my own FileStream class which would use...
6
by: meyzhong | last post by:
Hi, I am new to Visual C++ (6.0). I want to put all the print information in different lines into a box. I create a EDIT dialogue box, and then add a CString variable (m_MSG) to the dialogue...
11
by: kimiraikkonen | last post by:
Hello, I'm very new to C# and just installed VC# 2005 express edition and i have VB 2005 express installed previously. But i saw a thing which may be called as a "Visual C# 2005 express" bug? ...
7
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.