How to Efficiently Read and Store Numbers from a File in C++?

  • Thread starter Thread starter cellec
  • Start date Start date
  • Tags Tags
    Argument
AI Thread Summary
The discussion revolves around two main questions regarding C++ programming. The first question addresses an issue with reading numbers from a file using an `ifstream`. The problem arises because the end-of-file (eof) flag is not set until an attempt to read past the end occurs, leading to an extra entry in the array. A suggested solution is to modify the reading loop to check for eof after attempting to read, ensuring that only the intended number of entries are captured.The second question involves the challenge of using runtime values as template arguments in C++. The compiler does not allow non-constant values as template parameters, which leads to a "catch-22" situation where the program requires runtime input but templates need compile-time constants. The discussion suggests that to handle dynamic memory allocation, one should create a pointer of the desired data type and use the `new` operator. However, it clarifies that there is no way to convert runtime values into compile-time constants for template parameters.
cellec
Messages
13
Reaction score
0
Two questions,
1) Can you tell me why
PHP:
ifstream f("f.txt"); //there are 2 numbers in file f
int count=0
while(!f.eof()){
f>>x[count];
count++;
}
Why does there exist to be x[2] ? How can I chaneg the code to get only two x[0],x[1];

2) Completely different though,
Are there any ways to "parse" the values i.e integers as the template arguments at runtime ? My VC++ 6.0 compiler complains that i can't pass the numbers directly like that at run time.

Thanks in advance,
-Fiona's twin__001
 
Computer science news on Phys.org
Because the stream doesn't recognize eof until after you've actually attempted to read past the end of the file. A better solution would be

Code:
while(1) {
f >> x[count];
if (f.eof()) break;
count++;
}

- Warren
 
Thanks chroot, -->lol
 
Anyone has any ideas about my second question ?
 
Well, with the limited info you provided I'd say your problem is you're trying to compile a program which relies on runtime values but the program requires those values before it can compile. Big catch-22 huh? Do a google search for "new C++ dynamic memory" and you'll find the answer. Here, a couple of links from said same search to get you started if I'm reading your problem correctly.

http://www.jive.nl/~kamphuis/eff_c/dmemory.html
http://cplus.about.com/library/weekly/aa072502b.htm
http://www.cplusplus.com/doc/tutorial/tut3-4.html

Basically, you need to create a pointer of the desired data type then use the new operator so your compiler knows you want to use runtime variables.

Hope this helped, good luck.
 
Last edited by a moderator:
faust9, I would like to make some questions,
1) What is Big-catch 22 ?
2) Thank faust9 very much for those links, but my problem I think is not about memory allocation...My program asks the user to input a number and it will output numbers circling with radius 5, around the input, all are of course integers. But I tried using template in my program and I would like to put that input number in something like Circle<num> which is where compiler give me lots of complaints...Do you know how I can do something like std::cout<<Circle<Num> ?
 
Template parameters must be constants; there is simply no way around this. While there are lots of clever ways to compute constants at compile-time, the fact remains that they are still constants; there is absolutely no way to grab a runtime value and turn it into a compile-time constant.
 
Big catch 22 might mean using 22 to have a big catch /--lol--\
 

Similar threads

Replies
23
Views
8K
Replies
8
Views
2K
Replies
5
Views
2K
Replies
13
Views
2K
Replies
11
Views
35K
Replies
6
Views
2K
Replies
2
Views
2K
Back
Top