Feb 12, 2011 at 7:15pm UTC
x never changes it's value, so it's always 0. What exactly are you trying to achieve here?
Feb 12, 2011 at 7:30pm UTC
@Framework: x becomes 4 when if finds 'name' in the txt file :)
@hamsterman : Somewhere I knew I was doing it the hard way again :p
Anyways ,you'd do it like this then ? 'cause in my case it gives errors :/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void SearchName()//Searches for 'name'
{
ifstream file("test.txt" ); //open test.txt
string str;
while (file >> str)
{
if (str == "name" )
{
if (file >> str)
{
cout << str;
}
else cout << "that was the last word\n" ;
}
}
}
And thanks for the replies :)
Edit: sorry forgot to include the header ... anyways, it doesn't display the word after 'name' atm
Last edited on Feb 12, 2011 at 7:36pm UTC
Feb 12, 2011 at 7:41pm UTC
My mind is still foggy on what you're trying to achieve. What do you do with the data extracted from the file stream?
Feb 12, 2011 at 8:12pm UTC
Sounds like a parsing job to me.
Feb 12, 2011 at 8:25pm UTC
Uhm it sort of is :p
But I'm trying to make this to gain more experiance with .txt editing.
So if you know how I can make this, please tell me then :)
Feb 12, 2011 at 8:53pm UTC
See if this works for you:
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#include <fstream>
#include <stdio.h>
#include <vector>
#include <string>
using std::ifstream;
using std::string;
using std::vector;
struct Person
{
Person( const string &ThisName, const int &ThisAge, const string &ThisGender )
{
Name = ThisName;
Age = ThisAge;
Gender = ThisGender;
}
string Name;
int Age;
string Gender;
};
vector < Person > People;
void LoadFile( const string &File )
{
ifstream Source;
// Open the file for reading.
Source.open( File.c_str( ) );
// Is the file open?
if ( Source.is_open( ) == false )
{
// Failed.
printf( "Failed to open the given file.\n" );
return ;
}
// Command buffer.
string Command;
// File field buffers.
string NewName( "NULL" ), NewGender( "NULL" );
int NewAge( 0 );
// Extract data for 1 person.
for ( unsigned Pass( 0 ); Pass < 1; Pass++ )
{
for ( unsigned Field( 0 ); Field < 3; Field++ )
{
Source >> Command;
if ( Command == "Person" )
{
// Extract the name.
Source >> NewName;
}
else if ( Command == "Age" )
{
// Extract the age.
Source >> NewAge;
}
else if ( Command == "Gender" )
{
// Extract the gender.
Source >> NewGender;
}
else
{
// Assume it's a comment.
}
}
Person NewPerson( NewName, NewAge, NewGender );
// Add the person.
People.push_back( NewPerson );
}
Source.close( );
}
int main( )
{
LoadFile( "File.txt" );
for ( unsigned Index( 0 ); Index < People.size( ); Index++ )
{
printf( "Name:\t\t%s.\n" , People.at( Index ).Name.c_str( ) );
printf( "Age:\t\t%i.\n" , People.at( Index ).Age );
printf( "Gender:\t\t%s.\n" , People.at( Index ).Gender.c_str( ) );
}
return 0;
}
Edit-------------------------8<------------------------
This is the file I was reading from.
Person Jos
Age 30
Gender male
Last edited on Feb 12, 2011 at 8:55pm UTC
Feb 12, 2011 at 9:26pm UTC
Hmm, I suppose this does answer my example, but with my current programming experiance, this looks like a completely different language :o.
Is it possable to make this with these headers ?
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
Kind of learned c++ with these :)
And thanks for the replies
Feb 12, 2011 at 9:31pm UTC
It is possible. If you don't use the vector then you will have to improvise with a dynamic array or just miss-out the array completely.
Feb 12, 2011 at 9:49pm UTC
Faff, my code wasn't tested, but I don't see why it wouldn't work.. Could you tell me what it did print?
Framework, avoid mixing iostream with stdio. It's just weird to use two libraries for one thing.. Also, the correct name for stdio in c++ is <cstdio>.
Feb 13, 2011 at 12:09pm UTC
@hamsterman: it worked :) , sry seems like the txt file contained nameJos ...
Really Thanks! :D
But now, the original problem :)
How can I make it print nothing (or error msg) if the after the 'name' the line ends?
assume this txt file:
name Jos
name
name bla bla
name
Prog should output this :)
Current prog outputs this now :/
Josnamethat was the last word
How could I do that ?
Last edited on Feb 13, 2011 at 12:21pm UTC