Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So apparently I can\'t read data from the txt file. The data is in the form that

ID: 3838883 • Letter: S

Question

So apparently I can't read data from the txt file.

The data is in the form that shown below

# Size of rectangular area, in kms

0.50 0.25

# Number of objects in the area (type, size, x and y coords)

6

square 10 55 65

rectangle 20 15 410 157.5

circle 25 145 185

circle 15 415 75

square 15 227.5 27.5

rectangle 20 30 500 140

# Side of entry square, in metres

20

So how can I read the file for the square or rectangle one?

The code I have written at the moment is also shown below:

function data = readFile(filename)

fD = fopen(filename, 'r'); % r: open file for reading

line = fgetl(fD);

index = 1;

data = cell([8, 1]);

while line ~= -1

if line(1) == '#'

line = fgetl(fD);

else

ca = textscan(line, '%f');

data(index) = ca;

index = index + 1;

line = fgetl(fD);

end

end

end

Explanation / Answer

//Please find below the code for reading a text file line by line using C++

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt"); // Please specify the file path here
if (myfile.is_open())
{
    while ( getline (myfile,line) )
    {
      cout << line << ' ';
    }
    myfile.close();
}

else cout << "Unable to open file";

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote