Read in numbers and strings from a txt file

Hi all,

So if I want to read in something and store them in to different arrays (say two types of stuff are int and char), is there a way to read in the required parts of the file (requirements known in advance) and store them? I got pretty confused about this..
1
2
3
4
5
6
<1>
adadsada [armor] randomwords
<234>
yeah [come]
[okay] whatever
<-1>

If I want to read in the stuff in <> and [].. And I want to make them into two different arrays. Is there a way(without using STL stuff, only allowed with iostream fstream and sstream) to make it happen?
PS: read ends when reaches <-1>
PPS: I do not know the actual size required for the array so I will resize it. My working progress on this is here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int main (int argc, char** argv)
{

ifstream input(argv[1]);

char buffer[41];				

char** index = new char* [10];

int entries = 0;

input >> buffer;
	
index[entries] = new char[strlen(buffer)+1]; strlen-> string length
				adding one more place to the entries since the null terminator.

strcopy(index[entries],buffer);

index [entries][strlen(buffer)] = "\o";				



}


void resizeCharStarStar(char** &oldArr, int size)
{
	char** temp = new char* [size];
	for(int i = 0; i < size - 10; i++)
		temp[i] = oldArr[i];
		resize them by move oldArr to new structure.
	delete [] oldArr;
	oldArr = temp; // redirect the oldArr ptr to point to the current temp resized array.
}





Here is another attempt on the read in stuff. Wont work apparently...Plz Help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>


using namespace std;

int main(int argc, char* argv[])
{
    if(argc != 3)
        cout<< "Error! Not enough file!"<<endl;
    int** page = new int*[];
    char** words = new char*[];

}
//--------------------------------------------------------- 
void readInput(int** page1, char** name1){
    istream is;
    char par1, par2;
    int  usefulVal;
    is >> par1 >> usefulVal >> par2;

    // check if any input
    if(!is) return is;

    // check for valid input format
    if (par1 != '<' || par2 != '>'){
    // set failbit to indicate invalid input format
       is.clear(ios_base::failbit);

    // assign input values to second argument
    page1(usefulVal);

    char par_1, par_2;
    string string_value;
    is >> par1 >> string_value >> par2;
    if(!is) return is;
    if (par_1 != '[' || par_2 != ']')
    {
       is.clear(ios_base::failbit);
       return is;
    }
    name1(string_value);
    return is;
}
Last edited on
Some ideas will be very appreciated!
How about this, use getline store in string, find occurence of < or [ then mark the loation, if its found find its > or ] mark this one too, then use substring start from position of < or [ to position of > or ], then store it to array
can you show me a simple example? I'm confused with the steps.....thank you!
some help will be appreciated!....very urgent....
Keeping in mind I am a noob, there may be a better way of doing this.
But let's give it a shot. Here is a little mix of code - pseudo code to get you started.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Open your pre-processor directives


    std::string name, desc = "";     // Declare and initialize name and desc strings, assign them a blank value.
    int id, weight, loc = 0;              // Declare and initialize intigers id, weight, and loc, and give them the value zero.
    std::ifstream thefile;                 // Declare inout file stream and name the handle 'thefile'
    thefile.open("Itemdata.dat");     // Open the file Itemdata.dat for reading  (your text file)
    if (thefile.is_open()){                // Check to ensure file is open.

        while (thefile.good()){
            getline(thefile,name,'\}'); // Get the first line and assign it to the string name.
            thefile.ignore(1,'\0');
            getline(thefile,desc,'\}'); // Get the next line and assign it to the string desc.
            thefile.ignore(1,'\0');
            thefile >> id >> weight >> loc; // Get the next three integer values and assign them accordingly.
             thefile.ignore(1,'\0');

             // Here you add them to your container(s)   

  }  
        } // If file has not reached the end (still open) , repeat.
    thefile.close(); // close the file



This will:
1. declare your variables
2. open the file,
3. read the string
4. read integers

Now that you have the file read and stored into variables, you can place them into any container you wish. There is still more you need to add, however that should be enough to get you started.

Your text file would look like

String - Item name
String - Description of item
int int int - id, weight, location

or in a real file something like this...

1
2
3
4
5
6
7
8
9
     dagger}
     The dagger is about 6 inches long, with a rusty blade that hasn't been sharpened in a while. The handle is still in good condition and it appears it can be used for combat.}
     1 1 1
     dragonscale}
     This is the finest dragonscale armor you have ever seen. It lookes to be hand woven in the kingdom of the dwarves.}
     2 20 1
     torch}
     rags wrapped around a 3 foot wooden stick}
     3 2 12 

and so on...
Last edited on
Is this what you are asking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <fstream.h>
#include <conio.h>
// conio.h is used for clrscr() and getch()

void main()
{
clrscr();
int numbers[100],str=0,num=0,n,i;
char strings[100][15],s,ch;
//Creating a desired text file
ofstream out("file.txt");
out<<"Change settings at your own risk."<<endl;
out<<"[Health]  <100>"<<endl;
out<<"[Armor]  <200>"<<endl;
out<<"[DefaultWeapon]  [Sword,HandGun]"<<endl;
out<<"[CriticalDamage]  <150>%"<<endl;

//Begining the search
ifstream in("file.txt");
while(!in.eof())
{ in>>ch;
  if(ch=='<')
  { in>>n;  numbers[num]=n; num++;}
  else if(ch=='[')
       {
       for(i=0;;i++)
	 { in>>s;
	   if(s==']') break;
	   else strings[str][i]=s;
	 }
       str++;
       }
}

//Displaying both the arrays
cout<<"Numbers: ";
for(i=0;i<num;i++)
cout<<numbers[i]<<" ";
cout<<"\n\nStrings:\n";
for(i=0;i<str;i++)
cout<<strings[i]<<'\n';
getch();
}
Last edited on
Topic archived. No new replies allowed.