Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,724 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,322 people online right now. Registration is fast and FREE... Join Now!




Read a file

 
Reply to this topicStart new topic

Read a file

jayhuang
post 3 Nov, 2006 - 10:45 PM
Post #1


New D.I.C Head

*
Joined: 11 Oct, 2006
Posts: 31


My Contributions


I tried to read a file using the following code, but it kept saying it couldn't open the file. no idea where I missed. Could someone debug it, thanks ahead.
CODE

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


int main()
{
  int container[2000];
  fstream file_open("c:\\test_file.txt",ios::in);
  while(file_open >> container)
    cout << container;

  file_open.close();

  return 0;
}


EDIT : CODE Tags Added -b2c
User is offlineProfile CardPM

Go to the top of the page

gregoryH
post 4 Nov, 2006 - 12:06 AM
Post #2


D.I.C Regular

Group Icon
Joined: 4 Oct, 2006
Posts: 417



Dream Kudos: 50
My Contributions


QUOTE(jayhuang @ 3 Nov, 2006 - 11:45 PM) *

I tried to read a file using the following code, but it kept saying it couldn't open the file. no idea where I missed. Could someone debug it, thanks ahead.

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


int main()
{
int container[2000];
fstream file_open("c:\\test_file.txt",ios::in);
while(file_open >> container)
cout << container;

file_open.close();

return 0;
}

HI Jay

at first glance the code appears ok... best guess is the file isn't located in the root of the C drive. Check the location of the file.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 4 Nov, 2006 - 03:34 AM
Post #3


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


the fstream class does not contain a function that overloads the >> operator which takes an integer array on the right hand side.

Rename int to char.
Your code should look like this.
CODE

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


int main()
{
char container[2000];
fstream file_open("c:\\test_file.txt",ios::in);
while(file_open >> container)
cout << container;

file_open.close();

return 0;
}


The code compiles, and runs perfectly well. smile.gif
User is offlineProfile CardPM

Go to the top of the page

gregoryH
post 4 Nov, 2006 - 03:55 AM
Post #4


D.I.C Regular

Group Icon
Joined: 4 Oct, 2006
Posts: 417



Dream Kudos: 50
My Contributions


QUOTE(born2c0de @ 4 Nov, 2006 - 04:34 AM) *

the fstream class does not contain a function that overloads the >> operator which takes an integer array on the right hand side.

Rename int to char.
Your code should look like this.
CODE

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


int main()
{
char container[2000];
fstream file_open("c:\\test_file.txt",ios::in);
while(file_open >> container)
cout << container;

file_open.close();

return 0;
}


The code compiles, and runs perfectly well. smile.gif

Sanchit

I completely missed that one d'oh!... yes - I concur smile.gif

This post has been edited by gregoryH: 4 Nov, 2006 - 03:56 AM
User is offlineProfile CardPM

Go to the top of the page

jayhuang
post 5 Nov, 2006 - 09:25 AM
Post #5


New D.I.C Head

*
Joined: 11 Oct, 2006
Posts: 31


My Contributions


QUOTE(born2c0de @ 4 Nov, 2006 - 04:34 AM) *

the fstream class does not contain a function that overloads the >> operator which takes an integer array on the right hand side.

Rename int to char.
Your code should look like this.
CODE

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


int main()
{
char container[2000];
fstream file_open("c:\\test_file.txt",ios::in);
while(file_open >> container)
cout << container;

file_open.close();

return 0;
}


The code compiles, and runs perfectly well. smile.gif


Thank you for your help. B2C and gregory

This post has been edited by jayhuang: 5 Nov, 2006 - 09:26 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 10:15AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month