473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading string from file

1
Program in C++ on Linux.
I want to extract the strings string1 and string2 from the line:
config.?.getstring=("string1,string2");

This line is in another file. There are 100 other lines like this in the file that start with config but only "getstring" is unique. My program should not be aware of what string1 and string2 nor how many strings there are.

My program should pick up string1, sring2 etc and assign variables to them.

My code so far:
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. using std::string;
  7.  
  8.     int main()
  9.     {
  10.       string tmp = "getstring";
  11.       string tmp1;
  12.       fstream file_op("/path/of/file",ios::in);
  13.     if (!file_op)
  14.       {       
  15.         cout << "cant open file\n";
  16.         return 1;
  17.       }
  18.  
  19.         while (! file_op.eof())
  20.           {
  21.         getline(file_op,tmp1);
  22.  
  23.          cout << tmp1 << endl;
  24.  
  25.                                  //string tmp2; not working says return type should be char! 
  26.         // tmp2 = strstr (tmp1, tmp);
  27.         // cout << tmp2 << endl; 
  28.        // string sub = theline.substr(18);
  29.        // string tokens = strtok(sub," ,)(;");
  30.        // cout << sub << "\n";          
  31.           }
  32.         file_op.close();
  33.  
  34.         return 0;
  35.     }
Right now the program just opens the file and reads through it ...is there a way of specifing which line to get throught the getline function? Like saying the line contains "getstring" so pull out that line?
I am struggling with the strstr function. Also i dont know how i am supposed to find out the number of strings in that line and assign variables to them.

Please assist

Thank you
Mar 29 '07 #1
2 1671
May be you can use fseek() like below
/* fseek example */
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5.   FILE * pFile;
  6.   pFile = fopen ( "myfile.txt" , "w" );
  7.   fputs ( "This is an apple." , pFile );
  8.   fseek ( pFile , 9 , SEEK_SET );
  9.   fputs ( " sam" , pFile );
  10.   fclose ( pFile );
  11.   return 0;
  12. }
Mar 30 '07 #2
Ganon11
3,652 Expert 2GB
The above code is written in C, but you should be able to get it working if you change the include statement to

Expand|Select|Wrap|Line Numbers
  1. #include <cstdio>
Mar 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.