Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Read Numeric Data from a Text File in C++

Read Numeric Data from a Text File in C++

August 23, 2021 by James Palmer

Repeat >> reads in loop.
#include
#include
int main(int argc, char * argv[])
{
std::fstream myfile(“D:\data.txt”, std::ios_base::in);

float a;
while (myfile >> a)
{
printf(“%f “, a);
}

getchar();

return 0;
}

Result:
45.779999 67.900002 87.000000 34.889999 346.000000 0.980000
If you know exactly, how many elements there are in a file, you can chain >> operator:
int main(int argc, char * argv[])
{
std::fstream myfile(“D:\data.txt”, std::ios_base::in);

float a, b, c, d, e, f;

myfile >> a >> b >> c >> d >> e >> f;

printf(“%ft%ft%ft%ft%ft%fn”, a, b, c, d, e, f);

getchar();

return 0;
}

Edit: In response to your comments in main question.
You have two options.

You can run previous code in a loop (or two loops) and throw away a defined number of values – for example, if you need the value at point (97, 60), you have to skip 5996 (= 60 * 100 + 96) values and use the last one. This will work if you’re interested only in specified value.
You can load the data into an array – as Jerry Coffin sugested. He already gave you quite nice class, which will solve the problem. Alternatively, you can use simple array to store the data.

Edit: How to skip values in file
To choose the 1234th value, use the following code:
int skipped = 1233;
for (int i = 0; i < skipped; i++) { float tmp; myfile >> tmp;
}
myfile >> value;

It can depend, especially on whether your file will have the same number of items on each row or not. If it will, then you probably want a 2D matrix class of some sort, usually something like this:
class array2D {
std::vector data;
size_t columns;
public:
array2D(size_t x, size_t y) : columns(x), data(x*y) {}

double &operator(size_t x, size_t y) {
return data[y*columns+x];
}
};

Note that as it’s written, this assumes you know the size you’ll need up-front. That can be avoided, but the code gets a little larger and more complex.
In any case, to read the numbers and maintain the original structure, you’d typically read a line at a time into a string, then use a stringstream to read numbers from the line. This lets you store the data from each line into a separate row in your array.
If you don’t know the size ahead of time or (especially) if different rows might not all contain the same number of numbers:
11 12 13
23 34 56 78

You might want to use a std::vector > instead. This does impose some overhead, but if different rows may have different sizes, it’s an easy way to do the job.
std::vector > numbers;

std::string temp;

while (std::getline(infile, temp)) {
std::istringstream buffer(temp);
std::vector line((std::istream_iterator(buffer)),
std::istream_iterator());

numbers.push_back(line);
}

…or, with a modern (C++11) compiler, you can use brackets for line’s initialization:
std::vector line{std::istream_iterator(buffer),
std::istream_iterator()};

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in