Question

In C++, how would I read data from a .txt file into an array? For example,...

In C++, how would I read data from a .txt file into an array?

For example, I have an assignment that requires me to read temperatures from a file named data.txt into an array. How would I go about doing this?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
 *  C++ Program to read temperature values from a files into an array
 */

#include <iostream>
#include <fstream>
using namespace std;
#define MAX 100

int main()
{
  int i = 0, tempArr[MAX];

  fstream file;
  file.open("data.txt", ios :: in);

  while (!file.eof())
  {
    file >> tempArr[i++];
  }

  int count = i;

  for (i = 0; i < count; i++)
  {
    cout << tempArr[i] << endl;
  }

  file.close();
  
  return 0;
}

Add a comment
Know the answer?
Add Answer to:
In C++, how would I read data from a .txt file into an array? For example,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ that read a list of numbers from a .txt file into array. The...

    Write a C++ that read a list of numbers from a .txt file into array. The first digit on the list will be array size, while the second number on the list will be our first number in our array list. Example: t1.txt file contain list off input number: 5, 3, 6, 10, 43, 23. notice the first number is 5 which will need to be use as an array size and the rest of the numbers are in the...

  • Write a C++ program that will read a large .txt file and will find the highest...

    Write a C++ program that will read a large .txt file and will find the highest and lowest voltage, along with the associated time and ampere. The text file is large 1,000,000 rows of data, so speed is a factor. Per some of my own research it may be best to use mmap (If I have the fastest program I will receive a significant amount of extra credit)? Someone also told me that binary can be fast, but I have...

  • Assume that an array A is given to you in a txt file. You should read...

    Assume that an array A is given to you in a txt file. You should read it. If you think this is a time-dependent data, print the number of local minimums in the array to a txt file. Local minimum means the element which is less than the previous element and the following element. Please do not use any additional library. For example, if A=[3, 2, 9, 8, 7, 8, 6], the code should print 2 and 7. (Local minimums...

  • Write a java program to read from a file “input4_02.txt” an integer N (0? N ?...

    Write a java program to read from a file “input4_02.txt” an integer N (0? N ? 100) and then read N double numbers from this file to an array. Write to file “output4_02.txt” this array but in the reverse order and also the sum of all elements in the array. For example File: “input4_02.txt” 5 1.30 2.22 4.00 17.60 3.14 File “output4_02.txt” 3.14 17.60 4.00 2.22 1.30 Sum = 28.26

  • txt file iuput in C. If there is a txt file which only contain one row...

    txt file iuput in C. If there is a txt file which only contain one row like 1 2 3 How can I read these three integers individually as a special value to pass to a integer i and the size of a two dimensional array like: int i = 1; array a[ 2 ][ 3 ] = {}; Thanks

  • You are required to open a file for input and read the data in that file...

    You are required to open a file for input and read the data in that file into an array (named data). Use the variable size to count how many elements in the file. Create the file yourself in notepad and call it data.txt so that it has the following numbers in order: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 Please help with this. In C please

  • c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detec...

    c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detected. (An example of such a file is shown below). Also, the program should copy from values[ 1 any value which has even sum of digits and its index (location) to two new arrays evenArr 1 and idxArrl I, respectively. In addition, the program must...

  • I am told to create a fillArray method that will read integers from the file "data.txt"...

    I am told to create a fillArray method that will read integers from the file "data.txt" and store them in the array. I'm told to assume that the file has no more than 100 items in it. This is what I have so far: public void fillArray() { int curVal;    Scanner input = null; try { input = new Scanner(new File("data.txt")); // set the current number of items in the array to zero while (input.hasNextInt()) { curVal = input.nextInt();...

  • using c and pointers only 1. You will read in two strings from a file cp4in_1.txt...

    using c and pointers only 1. You will read in two strings from a file cp4in_1.txt at a time (there will be 2n strings, and you will read them until EOF) and then do the following. You alternately take characters from the two strings and string them together and create a new string which you will store in a new string variable. You may assume that each string is no more than 20 characters long (not including the null terminator),...

  • In Python how to write from an array to a .txt file so that each row...

    In Python how to write from an array to a .txt file so that each row in the array is one row in the text file. Fill in all blank spaces in the array with a 0.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT