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

  • Thread starter Thread starter cellec
  • Start date Start date
  • Tags Tags
    Argument
Click For Summary

Discussion Overview

The discussion revolves around efficiently reading and storing numbers from a file in C++, as well as the challenges of using runtime values as template arguments. It includes technical explanations and clarifications regarding file input operations and template programming.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Fiona's twin__001 questions why the code reads past the end of the file, leading to an extra element in the array, and seeks a solution to limit the read to only two numbers.
  • Warren explains that the stream does not recognize the end of the file until an attempt to read past it is made, suggesting a loop that checks for eof after each read.
  • Fiona's twin__001 asks for further clarification on the second question regarding parsing runtime values as template arguments, indicating issues with the VC++ 6.0 compiler.
  • faust9 suggests that the problem may stem from trying to use runtime values in a context that requires compile-time constants, recommending dynamic memory allocation as a potential solution.
  • Another participant, faust9, provides links for further reading on dynamic memory in C++.
  • Fiona's twin__001 clarifies that their issue is not about memory allocation but rather about using user input as a template argument in a class.
  • A participant states that template parameters must be constants and that there is no way to use runtime values as compile-time constants.
  • There is a light-hearted exchange regarding the term "Big catch-22," with some humor added to the discussion.

Areas of Agreement / Disagreement

Participants express differing views on the nature of the problem, with some focusing on file reading mechanics and others on the constraints of template programming. The discussion remains unresolved regarding the best approach to handle runtime values in templates.

Contextual Notes

There are limitations regarding the assumptions made about the code's behavior and the specific requirements of the compiler being used. The discussion does not resolve the technical challenges presented.

Who May Find This Useful

Readers interested in C++ programming, particularly in file I/O operations and template programming, may find this discussion relevant.

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::count<<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 ·
Replies
23
Views
9K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
4
Views
15K
  • · Replies 11 ·
Replies
11
Views
35K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 24 ·
Replies
24
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K