Ok, a few questions on fstream with C++ and winsock

1. Is it possible to search all the tagged lines that say Item I.D. for a specific value that is a variable such as CharacterItemID is 108 than search all the tagged lines that say Item I.D. for 108 and read the data.

2. Also is it possible to specifically send a variable such as a string or integer with sockets open on the client and the server from the client to the server and the server will know to put it in a specific variable on its own or does sockets not work that way. I mean how does a server know what to do when it receives an integer variable of 8 from a client. Can you make it so it knows when receiving certain data to store it in certain variables. Sorry if a little confusing.

All help is appreciated.

1. Yes. just use standard stream file i/o to sequentially read the file one line at a time. Look for the tag name, when found look for the value on that line.

2. Yes. The programmer has to write the server program in such a way that it knows what to do the the information it gets from the client.

can something like this be read in fstream though and so if players item id is 1 it searches the 1 section and collects the data in there and so on for each item id, this would be save in something like items.txt. Can someone explain how to read that one

[1]
Name=Sword
STR= 8
DEF= 6
SPEED= 11
MAGI= 0

[2]
Name=Axe
STR= 8
DEF= 6
SPEED= 11
MAGI= 0

The way I would do it is to create a string that contains the player ID that looks just like it is in the file

#include <string>
#include <sstream>
using namespace std:

int main()
{

   int PlayerID = 1;
   stringstream str;
   str << "[" << PlayerID << "]:
   string player;
   str >> player; // this is now "[1]"

Now all you have to do is write the code to read the file looking for the player string that was created in the above code. When it is found, you can easily process read all the information associated with that player.

Will I write the code for you? The answer is NO. But if you post the code you have tried then we will try to help you with it.

sounds good ill post it if i have problems

ok sorry to be a noob but I'm getting screwed up badly, this is the code of a small program I am writing to test what I am trying to implement. This part of the code works, when I throw in your code I get weird answers and can't really fix it because I don't understand your code or the point of it. I need some kind of explanation.

#include <iostream>
#include <fstream>

using namespace std;




int main()
{
    ifstream file;
    file.open("Items.txt");
    string itemname;


    cout << itemname;
    system("PAUSE");
}

The point of the code I posted is to create a string that can be compared to the tag in the file. It can be used like this:

create the string named player as I previously posted
open input file
while not end of file
   read a line
   Is this the same as the player string (e.g. "[1]") ?
   If yes then begin this loop
          read STR
          read DEF
          read SPEED
          read MAGI
          exit both loops
     end of loop
end of while loop

Ok, I am starting to understand but I have no idea the syntax for reading STR and DEF and all that. I mean ok so I find a line that is [1] than how do I go down 1 line read the data and down another read the data and so on?

>>how do I go down 1 line read the data and down another read the data and so on?
getline() will do that for you

std::string line;
ifstream in("items.txt");
// find the line is not shown here

// each time the line below is executed the program will read the next line in the file
getline(in, line);

ok I have done some design changes to make it easier. In Items.txt there is now a format.

- First line is Item ID
- Second Line is Item Name
- Third Line is Item Damage

Would it be possible to have a program ask you for Item ID which would be stored an integer. Than start at the first line check it and store it in a temporary variable. Check if the Item ID and result match and if they do go one line down read the item name and store it to a string than one more down and store damage as an integer. The problem I had when I tried this was that I don't know how to read an integer from a line instead of getline which is just for strings right? So instead of comparing strings im comparing integers.

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

int main()
{
    string ItemName;
    int ItemID;
    int CheckItemID;
    int ItemDamagePower;
    cout << "Enter Item ID: ";
    cin.get();
    cin >> ItemID;
    ifstream file;
    file.open("Items.txt");
    getline(file,CheckItemID);
    if (CheckItemID == ItemID)
       {
        getline(file,ItemName);
        getline(file,ItemDamagePower);
        cout << "\n";
        cout << "\n";
        cout << ItemName;
        system("PAUSE");
    }
    else
    cout << "Item does not exist";
    system("PAUSE");
}

>>Would it be possible to have a program ask you for Item ID
You are posting the wrong question -- that is a YES or NO question. And the answer is Yes.

>>getline which is just for strings right?
Wrong. Everything in a file can be read as strings, both numbers and characters. If you open the file in Notepad.exe all you will see is text. getline() does not distinguish between text and numbers, its all text as far as getline() is concerned.

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

int main()
{
    int ItemID;
    int CheckItemID;
    string ItemName;
    int ItemDamagePower;
    cout << "Enter Item ID: ";
    cin.get();
    cin >> ItemID;
    ifstream file;
    file.open("Items.txt");
    getline(file,CheckItemID);
    if (CheckItemID == ItemID)
        getline(file,ItemName);
        getline(file,ItemDamagePower);
        cout << "\n";
        cout << "\n";
        cout << ItemName;
        system("PAUSE");

    else
        cout << "Item does not exist";
        system("PAUSE");
}

the code is giving me 4 errors, here they are

C:\Documents and Settings\Cam\My Documents\project1\main.cpp||In function `int main()':|
C:\Documents and Settings\Cam\My Documents\project1\main.cpp|17|error: no matching function for call to `getline(std::ifstream&, int&)'|
C:\Documents and Settings\Cam\My Documents\project1\main.cpp|20|error: no matching function for call to `getline(std::ifstream&, int&)'|
C:\Documents and Settings\Cam\My Documents\project1\main.cpp|26|error: expected primary-expression before "else"|
C:\Documents and Settings\Cam\My Documents\project1\main.cpp|26|error: expected `;' before "else"|
||=== Build finished: 4 errors, 0 warnings ===|

I have no idea what went wrong

As I have said before, getline() takes a std::string as the second parameter and you are passing it an integer. Doesn't work like that. See the example in my post #9 above.

If you just want to read an integers then you can use the stream's >> operator file >> ItemDamagePower;

if I use the stream's operator will it automatically put me on the next line?

Ok, now there is no errors but It is not displaying the results it runs and asks for item ID i type in 1 it does its stuff and puts my input thing on a new line and displays no results when it should.

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

int main()
{
    string ItemID;
    string CheckItemID;
    string ItemName;
    int ItemDamagePower;
    cout << "Enter Item ID: ";
    cin.get();
    cin >> ItemID;
    ifstream file;
    file.open("Items.txt");
    getline(file,CheckItemID);

    if (CheckItemID == ItemID)
        getline(file,ItemName);
        file >> ItemDamagePower;
        cout << "\n";
        cout << "\n";
        cout << ItemName;
        cout << ItemDamagePower;
        system("PAUSE");
}

line 13: delete because it isn't necessary.

line 17: What does the file look like now? Like what you originally posted ? Or did you change it like you said you were. You'll have to post the file so that we can see what the program is supposed to read.

this is the file from the first line

1
Sword
8
10

2
Axe
9
9

now that I took line 13 out its giving me about 10 numbers which doesnt make sense

now that I took line 13 out its giving me about 10 numbers which doesnt make sense

It doesn't make sence because the file doesn't make sense. How is your program supposed to know which line number(s) contains the ItemID ? Your original file format is the most logical, and the format all *.ini files use in the windows operating system. With that format all you have to do is read each line until is contains the string you want.

To continue the code I posted earlier:

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


int main()
{

   int PlayerID = 1;
   stringstream str;
   str << "[" << PlayerID << "]";
   string player;
   str >> player; // this is now "[1]"

   cout << player << "\n";

   std::string Name;
   int STR;
   ifstream in("Items.txt");
   std::string line;
   // read each line in the file until we find the
   // line that contains "[1]"
   while( getline(in, line) )
   {
        if(line == player)
        {
            //
            getline(in,line);
            // locate the = symbol then extract the remainder
            size_t pos = line.find('=');
            // extract Name
            Name = line.substr(pos+1);

            getline(in,line);
            // locate the = symbol then extract the remainder
            pos = line.find('=');
            // extract STR
            line = line.substr(pos+1);
            STR = atol(line.c_str());
            // now do similar with all the reset of the items

            // Now stop this loop
            break;
        }

   }
}

Ok thanks for ur help. Really appreciated after it took me 18 posts to understand :)

Ok, I read your comments and everything in the code you posted but what do these parts mean. Its hard to modify when I dont understand some of the syntax because its brand new to me

// locate the = symbol then extract the remainder
            size_t pos = line.find('=');
            // extract Name
            Name = line.substr(pos+1);

            getline(in,line);
            // locate the = symbol then extract the remainder
            pos = line.find('=');
            // extract STR
            line = line.substr(pos+1);
            STR = atol(line.c_str());

I think you need to start at the beginning with a tutorial about std::strings. Here (click this)is one of probably several you can find on the net. Bookmark that link so that you can refer to it often while you work with strings.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.