0

I have a program in which i open a a text file, access it's values and create and array with it. for example the text file may contain these values:

1 44 3 4 33 6 7 8 61 10

; that's 10 integers

what c++ tools could i use to count the number of integer values in the file? i want it so that it runs through the text file and counts 10 integers

This Question has been **Answered**

0

1 44 3 4 33 6 7 8 61 10

; that's 10 integers

what c++ tools could i use to count the number of integer values in the file? i want it so that it runs through the text file and counts 10 integers

A simple ifstream should suffice. Have a counter variable, initialize it to 0, set up a while loop that reads one integer at a time from the ifstream. Increment your counter variable inside the while loop.

0

If you merely want a tool, doesn't Microsoft Word have a statistics page indicating number of words in a document? Open the file with that.

I use ASCII parsers all the time. ASE file readers, etc. I actually treat it as a gigantic ASCIIz buffer. Get File size allocate size + 1 byte. Set that last byte to 0; Now its a gigantic string. I parse ASCII files as get a word, skip space, skip line. So nomatter what the data is it skips leading space, collects the next block of characters separated by white space and then returns with it in a buffer. I then parse that buffer for what kind of data is expected. Very modular, and easy to work with.

As to your post, then its merely a matter of counting parsed ASCII data blocks as they're parsed!

0

A simple ifstream should suffice. Have a counter variable, initialize it to 0, set up a while loop that reads one integer at a time from the ifstream. Increment your counter variable inside the while loop.

ok here is my program so far

int day*;
int score[j];*

void deeznutz()

{

char inputFilename[] = "in.list";

ifstream inFile;

int day;

int score;

inFile.open(data.txt, ios::in);

if (!inFile) {

cerr << "Can't open input file " << data.txt << endl;

exit(1);

}

while (inFile >> day >> score) {

outFile << day = day* << " " << score = score[j] << endl;*

}

so let's say the file has 10 integers. what i would like the program to do is go through the file and see that is does have 10 integers, it now has 10 stored as a value, divide it by 2. so now it will make it into a 5X2 array, with i being the day and j being the score. what's the syntax for a counter? i know it's a loop. thanks for the help

0

Unless your assignment required an array, you can merely sum their values until the last one was read then do the division for the averaging!

If it does require an array, you can do a two pass. Run it once to count the number of integers in the file. Rewind, allocate the array, then run it again filling the array.

Or the growing cache method.

Start with a fixed size dynamic array. Filled level, vs array size. When the array becomes filled. Allocate another + grow size, copy the elements from old array to new array, then delete old array. Continue, until all numbers collected.

Think of this last one as an inch worm. Worm reaches out, touches ground then pulls up its tail towards the head.

0

This seems to be a different spec than originally mentioned. Here's what I originally had in mind:

```
const int MAX_SIZE = 100;
int array[MAX_SIZE];
int numIntegers = 0;
while (inFile >> array[numIntegers])
{
numIntegers++;
}
```

This assumes everything in the file is an integer and there are at most 100 of them.

Now everything is paired? Is that what is happening? So if you have ten integers, you have five "records", each record consisting of two integers, the first being the day, the second being the score? And you want a 2-dimensional array? You may need to elaborate. What can you assume as far as the maximum number of records? If you can assume something small like 100, do it as above. If you can't, you'll need to dynamically allocate the array as wildgoose says, or use a vector or something else that does it for you.

So repost with more detail please, and use code tags for your code.

*Edited
by happygeek*: fixed formatting

0

so basically the project is; everyday i am given a textfile that contains a certain amount of integers, i don't know how many, it could be 30 or 1000, all i know is that it is an even number. the odd numbers( i.e. the 1st, 3rd and 5th integers) are the "day" and the even integers are the "score". so everyday i am given a text file with x amount of integers, and i am to produce an (x/2) by 2 array(i.e. i am given 10 integers in a file so it will produce a 5 by 2 array). so my thought process is first open with file with inflie. and then establish a counter. if i count the total number of integers, i can just divide the number by 2. is that correct flow in your opinion? thank you for the help,

*Edited
by happygeek*: fixed formatting

0

```
#include <iostream.h>
#include <fstream.h>
void deeznutz()
{
char inputFilename[] = "data.txt";
ifstream inFile;
int x;
inFile.open("data.txt", ios::in);
if (!inFile) {
cerr << "Can't open input file " << "data.txt" << endl;
exit(1);
}
const int MAX_SIZE = 100;
int array[MAX_SIZE];
int numIntegers = 0 ;
while (inFile >> x)
{
array[numIntegers] = x;
numIntegers++;
}
inFile.close();
cout<< numIntegers << endl;
}
```

that is my code so far , it goes through the file and tells you how many values are in the file. what i want it to do now is produce an array based on this value. an array with a column value of 2. just like i said before if there are 10 integer values in the file it will create a 5 by 2 array. so now this is my predicament, how do i go about doing that? any help is greatly appreciated.

0

Why not have a 2-Dimensional array? That's what you are supposed to do anyway, right?

```
const int MAX_SIZE = 100;
int array[MAX_SIZE][2];
int numRecords = 0 ;
while (inFile >> array[numRecords][0] >> array[numRecords][1])
{
numRecords++;
}
```

0

Why not have a 2-Dimensional array? That's what you are supposed to do anyway, right?

`const int MAX_SIZE = 100; int array[MAX_SIZE][2]; int numRecords = 0 ; while (inFile >> array[numRecords][0] >> array[numRecords][1]) { numRecords++; }`

thank you very much for your help. it has helped me to understand c++ more. I just started C++ 3 weeks ago. i stated my problem incorrectly. as well as count the number of integers in the text file, rather than make the x/2 by 2 array it's supposed to make 2 separate arrays, one with the odd ordered integers as "day" and even orders integers as "score". so the flow is, it goes through the file and counts the integers and tells you the number, next it is to produce a score array and a day array. for that i am just assuming to stick this into the code

```
int day;
int score;
int day1[x/2];
int score1[x/2]
while (inFile >> day >> score)
{day=day1[i] << score= score1[j] << endl;
}
```

how does this look?

0

thank you very much for your help. it has helped me to understand c++ more. I just started C++ 3 weeks ago. i stated my problem incorrectly. as well as count the number of integers in the text file, rather than make the x/2 by 2 array it's supposed to make 2 separate arrays, one with the odd ordered integers as "day" and even orders integers as "score". so the flow is, it goes through the file and counts the integers and tells you the number, next it is to produce a score array and a day array. for that i am just assuming to stick this into the code

`int day; int score; int day1[x/2]; int score1[x/2] while (inFile >> day >> score) {day=day1[i] << score= score1[j] << endl; }`

how does this look?

I assume x is defined somewhere? The line in red above doesn't make sense. You have << and endl, but no cout statement, plus you have the assignment operator. I think you want this:

```
const int MAX_SIZE = 100; // assumes there are at most 100 records in file
int day[MAX_SIZE];
int score[MAX_SIZE];
int numRecords = 0;
// read in data
while (inFile >> day[numRecords] >> score[numRecords])
{
numRecords++;
}
// display data
cout << "Record # day score" << endl;
for (int i = 0; i < numRecords; i++)
{
cout << i << " " << day[i] << " " << score[i] << endl;
}
```

This reads and displays the data. The columns won't line up very well (you can use setw and left and right from the iomanip library to make the table line up), but this reads in your file into two separate arrays, keeps track of how many records there are, and displays them.

This question has already been answered. Start a new discussion instead.

Recommended Topics

I have a datagridvied with several columns and one with date and the format is

Me.DataGridView1.Columns("Doopdatum").DefaultCellStyle.Format = "yyyy - MM - dd dddd"

or

Me.DataGridView1.Columns("Doopdatum").DefaultCellStyle.Format = "dddd dd - MM ...

Hi DW.

Is there anyone know how to read a text file line by line from the web/internet in VC++?

I have a function which works well in reading the ...