how can I read specific data from file? let's say I have file containing this:

Data1: 1
Data2: 2
Data3: 3
Data4: 4

how can I make program find "Data1: " and then get everything what's behind it? or like in some configs, there is:

<Something: "something2",

how can I make program to insert "something2" (without quotes) into something[0] string and "something3" into something[1] string? so my program will get everything after "Something:" and between quotes

I saw that somewhere few months ago, that person was using stdio.h to open txt file (fopen) and then some command to look for specific word and then he got his value, but I can't find it now...

Recommended Answers

All 7 Replies

how can I read specific data from file?

I have designed my own method which involves stepping through each character of a text file once and recording the position of all the new lines, but I think someone else might have a more efficient method.

Or.. you can just push the entire text file into a vector (or something similar) and perform searches on the data ye' wish to find.

how can I make program to insert "something2" (without quotes) into something[0] string and "something3" into something[1] string? so my program will get everything after "Something:" and between quotes

using namespace std;

int main()
     fstream infile;
     int counter = 0;
     string text_file[10];


          cout << "\aError!  File not found.";
          cout << "\nFile may have been deleted, moved, or renamed.";
          cout << "\nPress [ENTER] to continue... ";

          infile >> text_file[counter];

     return 0;

well, now I have everything in string, how can I find something in it? I need to check every character anyway :X does anybody knows better method or that one I described?


Posing requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]To [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use your time wisely and [b]explain[/b] as best 
  you can.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
Think more about your next post so we don't have to play 20 questions to get the info we need.


1. I asked question properly

2. I don't have code, but ok:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
	string sFile[50];
	int iT = 0;
	ifstream File("Data.txt");
		cout << "File not found!";
			File >> sFile[iT];
	return 0;

and I have txt file containing this

aa: 1
bb: 2

3. now I need to get SPECIFIC word in Data.txt (let's say "aa") and then get its value (so I need to get that "1"), I already said that and I think that I explained my problem VERY GOOD

4. nothing because I have no idea how to do that thing.

5. I need a help how to do it, I don't wanna from you to do whole thing for me

7. I don't have any requirements, I just wanna know how to get that value using stdio.h cuz I saw it once here, on DaniWEB, but I can't find it anymore... so basically how to get values from configs...

if I recall correctly, the stdio.h library is just an antiquated C version of the modern <iostream> C++ version. it may or may not have file handling properties.. I don't know because I have almost no C experience; however, you should focus on modern coding practices, such as staying away from the old C .h libraries (generally speaking of course)

from what I understand, you wish to read in a line from a text file, and parse it into several useful parts. One method is to read in the text file, 'line at a time' and then split the line up into tokens. This method is useful when your text file may not always strictly adhere to a standard format (random text for example, like a love letter from your girlfriend)

string line;
string target;
int line_counter = 0;
int pos = 0;

//ask user what they be lookin' for
cout << "Enter something to find within' ye' text file: ";
cin >> target;

while(infile  && pos != string::npos)
     //set line counter

     //read in entire line of text
     getline(infile, line);
     //attempt to find what ye' be lookin' for (as in your example, ye' wish to find 'aa')
     pos = line.find(target);

     //if found
     if(pos != string::npos)
          cout << target << " found at position " << pos << " in line #" << line_counter << " of the text file. ";

//if not found
if(pos == string::npos)
     cout << target << " not found in ye' text file. ";

This method allows ye' to specifically test and identify specific parts of your text file, even if it's all random. The code I provided was based on this example.

Another way to get the parts ye' need out of ye' text file, is to read the entire file 'word at a time'. Using the >> extraction operator, words can be read in one at a time as long as they are 'white space' delimited. This works well for text files that have a standard format, such as the table you have provided because you can assume (for example) that the first word will be (in your case) the line number, second word will always be two letters and a colon, and the last 'word' will always be a number:

string target;
int pos = 0;
bool is_found = false;

//make a custom made container that can hold a line of text
struct line_struct
     string line_number;
     string two_letters;
     string a_number:

//now make a bunch of containers 
vector<line_struct> text_file;

//make a temporary container
line_struct temp;

//read the text file
     //load the temporary container
     //read in the text file, 'word at a time'
     infile >> temp.line_number;
     infile >> temp.two_letters;
     infile >> temp.a_number;

     //load the text_file vector with a 'line of text'

cout << "Enter something ye' wish to find in ye' text file: ";
cin >> target;

for(int i=0, size=text_file.size(); i<size; i++)
     if(target == text_file[i].line_number || target == text_file[i].two_letters || target == text_file[i].a_number)
          pos = i;
          is_found = true;

// not found
     cout << target << " not found in ye' text file. ";

else //found
     cout << target << " found in line " << pos << " of ye' text file. ";

You can neatly package your text file into nice containers and load them easily into a vector because you know your text file follows a specific format. Load the vector and perform user-inquiry based searches as needed.

thanks a lot! this helped me greatly, I'm using first example (I changed it a little) and it works nice!

commented: teehee. +10
Be a part of the DaniWeb community

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