-
September 9th, 2002, 05:55 AM
#1
need help (c/c++)
How can i read some formated data like what is below:
428 9131 1878 8147
7716 5775 2118 3488
1771 2796 2125 2267
5292 8886 8271 4902
6428 4837 9460 413
6002 4064 7462 1077
9800 7856 3231 1413
8465 2451 5489 3428
6283 8174 5063 4372
8806 5591 2940 8926
9349 8899 7675 5647
3861 4284 9671 2002
and assign each number to an array(a[0][0]-a[12][4]).
(The gaps between the numbers were made using the backslash character constant \t)
-
September 9th, 2002, 08:37 AM
#2
You can assign the data to the array at initialization. I used only the 5 first rows. You should put this code inside your .h-file:
static int data[5][4]={
{428, 9131, 1878, 8147} ,
{7716, 5775, 2118, 3488},
{1771, 2796, 2125, 2267},
{5292, 8886, 8271, 4902},
{6002, 4064, 7462, 1077}
};
-
September 9th, 2002, 09:34 AM
#3
if you have saved the data to a text file, you can use the function fopen to first open the file, for this you will need a understanding of pointers, next I would sugest useing the fscanf function to retrieve formated input from your file, you could create a incrementing loop to store each number retrieved into a array. Hope this helps.
-
September 9th, 2002, 10:45 AM
#4
well i forgot to say that i want to read this from a file and the problem i have is assigning the
number to the array
-----------------------------------------------------------------------------------------------------------------------
the game is not about how many steps you take it is about that only wrong step
-
September 9th, 2002, 11:52 AM
#5
You can use the <fstream.h>. I didn't actually try to compile this, but hopefully you'll get the idea. The ifs>>current will step through the file and stop for each word that is seperated by a space.
#include <iostream.h>
#include <fstream.h>
main()
ifstream ifs;
int current;
int myarray[12][4];
int counter1=0;
int counter2=0:
ifs.open("c:\\thefile.txt");
while(ifs>>current, !ifs.eof())
{
myarryay[counter2][counter1]=current;
counter1++;
if(counter1==5)
{
counter1=0;
counter2++;
}
}
-
March 24th, 2003, 09:21 PM
#6
Senior Member
In c++ you can use something like this. The array i used is incorrect but the basic idea should help. If you want i can post a similar program where i input from one file and output a result in another html file. Hope this helps
#include <iostream>
#include <fstream>
ifstream in_stream;
ofstream out_stream;
//open fileone and filetwo for input and output
in_stream.open("fileone.txt");
out_stream.open("filetwo.txt");
//to get the variables or strings, whatever you chose
i =0;
while (in_stream >> array[i]){
i++
}
in_stream.close( );
out_stream.close( );
Ben Franklin said it best. \"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.\"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|