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

  • Thread starter cellec
  • Start date
  • Tags
    Argument
In summary: If your program tries to access a variable that doesn't currently exist, you'll get an error at runtime.std::cout and std::string are two different types. std::string is a class, std::cout is a function that takes a std::string as an argument.std::cout doesn't print anything until you call the function, so the Circle<Num> thing would be impossible.
  • #1
cellec
13
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
  • #2
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
 
  • #3
Thanks chroot, -->lol
 
  • #4
Anyone has any ideas about my second question ?
 
  • #5
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:
  • #6
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> ?
 
  • #7
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.
 
  • #8
Big catch 22 might mean using 22 to have a big catch /--lol--\
 

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

1. What is a template argument?

A template argument is a placeholder or variable used in a template function or class that allows the same code to be applied to different data types or values.

2. How do template arguments work?

Template arguments are specified by the user when declaring a template function or class. They are then substituted for the template parameters in the function or class definition, allowing the code to be used for different types or values.

3. What is the purpose of using template arguments?

The purpose of using template arguments is to increase code reusability and efficiency. By using a single template function or class, the same code can be applied to multiple data types or values without having to write separate functions or classes for each type.

4. What is the difference between a template argument and a function argument?

A template argument is specified when declaring a template function or class, while a function argument is specified when calling the function. Template arguments are used to define the type or value that the function or class will operate on, while function arguments are used to pass in specific values to the function.

5. How does eof() work with template arguments?

eof() is a function used to check for the end-of-file (EOF) marker in a file. It can be used with template arguments to check for the end of a specific file type, such as a text file or binary file. The template argument would specify the type of data being read from the file, and eof() would return true when the end of that specific file type is reached.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
23
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
8
Views
909
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
14K
  • Programming and Computer Science
Replies
1
Views
977
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
Back
Top