Re: how to open a file (example .txt) using VC++??
Quote:
Originally Posted by scootz3345
the codes can run well, however the output format of the file is different from the input file..can anyone help me fix the code abit so that the output format is the same as the input format (such as paragraph in output is the same as paragraph in input)
Well...what is different? Missing line-breaks? In this case...
Code:
while(std::getline(in, t))
out << t << std::endl;
should help...
Besides that...if you simply want to copy a file there are easier methods... ;
Re: how to open a file (example .txt) using VC++??
thanx man...actually this is just a part of a program...:)
i am finding how to change the content of the opened file n come out with the output in a new file...
thanx for the help
Re: how to open a file (example .txt) using VC++??
whats the different between struct, pointer and array???
lets if i have words, name:my name is eric stored in a file a.txt
how can it be detected so that " my name is eric " can be stored in a variable "name" ??
can anyone help me???
Re: how to open a file (example .txt) using VC++??
The variable name has to be there during compiling, thus cannot be applied dynamically at run-time...other than that you simply parse your file...
Code:
#include <string>
#include <fstream>
#include <iostream>
int main()
{
// Open file
std::ifstream in("c:\\test.txt");
if(!in)
return -1; // Could not open file
// Read line-by-line
std::string t;
std::string name;
while(std::getline(in, t))
{
std::string::size_type pos = t.find("name:");
if(pos != std::string::npos)
name = t.substr(pos + 1);
}
// Close file
in.close();
std::cout << name << std::endl;
return 0;
}
Re: how to open a file (example .txt) using VC++??
thanx again for the code!
the code is compiled and run successfully, however it didn't produce any output...can u help me find whats wrong with it ??
really appreciate it!
Re: how to open a file (example .txt) using VC++??
Well...I would simply assume that there is no "name:" string in the file...can you upload the file here?
1 Attachment(s)
Re: how to open a file (example .txt) using VC++??
ok..thanx..i'll upload the file..
actually im just using the variable "name" as an example.
This is a sample of a syslog file.
Actually what im trying to do is, if u see in the example.txt, there is a word
"date=2005-04-26". I want the code to detect "date=" and return the
value of "2005-04-26" into a variable so that it can be sent to the
database field. And then also return other values such as "device_id"
and "log_id".
Thanx so much !!
Re: how to open a file (example .txt) using VC++??
Well...I don't see "date=" or anything else you mentioend in the file...however, you simply need to exchange the 'name:" in the 'find()' function to the appropriate search value you want...e.g. "date=".
Same goes for extracting the data....if they are in the middle of the line you need to calculate both the beginning (by using 'find()') and the end (by using 'find()' again for example)...
1 Attachment(s)
Re: how to open a file (example .txt) using VC++??
sorry..my bad...i have posted a wrong file...
this is the correct file..
can u write me an example using find() from this file??
Re: how to open a file (example .txt) using VC++??
I have managed to open the file n separate the content, however i have this problem where
display_string(buffer, "date=", 'a');
:
:
:
void display_string( char *buff, char *str, char flag )
{
char *plocation;
int pos=0;
char *tmp = buff;
char d[100];
char ff = flag;
int j=0;
int len = strlen(str);
switch(ff)
{
case 'a':
len=len+10;
break;
case 'b':
len=len+8;
break;
as u can see im reading it by counting the line after each word in case example "date=" ---len+10..10 character after the word "date=" will be consider as date..
however i have problems..what if we set it as 14..the date only contain of 10 so it will also display the balance of other 4 character..
it will display them as well..example "12-10-2005 sed" 14 character
how can i make it so that it will not take any other character after the space???
plocation = strstr( tmp, str);
pos = plocation - tmp;
if (plocation)
{ cout<<"\n\t";
//cout<<"\nThe time of this entry is:"<<endl;
for(int i=pos;i<pos+len;i++)
{
//do{
d[j]=buff[i];
//cout<<buff[i];
//}while (d[j]!=[ ]);
cout<<d[j];
j++;
}
//cout <<pos<<endl;
}
Re: how to open a file (example .txt) using VC++??
Change the following part of my previous code
Code:
// Read line-by-line
std::string t;
std::string pattern("date=");
std::string date;
// Get one line
while(std::getline(in, t))
{
// Find the beginning position in the string of the following pattern
std::string::size_type pos = t.find(pattern);
// Check whether we reached the end of the string (-> pattern not found)
if(pos != std::string::npos)
{
// Extract the value of the pattern. The value starts at the returned
// position plus the length of the pattern and ends at the next space.
// Thus, we need to find the next space beginning at pattern position.
// The returned position then needs to be recalculated so that we get
// the number of characters we want to extract. Thus, we subtract
// the beginning position plus the length of the pattern from the
// position of the space...
data = t.substr(pos + pattern.length() , t.find(" ", pos) - pos + pattern.length());
}
}
Not tested though....
1 Attachment(s)
Re: how to open a file (example .txt) using VC++??
there's 1 error:
this is the whole codes:
#include <string>
#include <fstream>
#include <iostream>
int main()
{
char data;
char name;
// Open file
std::ifstream in("samplesyslog.txt");
if(!in)
return -1; // Could not open file
// Read line-by-line
std::string t;
std::string pattern("date=");
std::string date;
while(std::getline(in, t))
{
std::string::size_type pos = t.find(pattern);
if(pos != std::string::npos)
{
data = t.substr(pos + pattern.length() , t.find(" ", pos) - pos + pattern.length());
}
}
// Close file
in.close();
std::cout << date << std::endl;
return 0;
}
This is the error:
C:\Documents and Settings\Administrator\My Documents\3 June 2005\adreas maseur\sample5.cpp(23) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' (or there is no acceptable conversion)
Error executing cl.exe.
sample5.obj - 1 error(s), 0 warning(s)
Please test it for me...thanx
Re: how to open a file (example .txt) using VC++??
It should be
Code:
date = t.substr(pos + pattern.length() , t.find(" ", pos) - pos + pattern.length());
Re: how to open a file (example .txt) using VC++??
Quote:
Originally Posted by scootz3345
whats the different between struct, pointer and array???
I think this should be nominated for the Question Of The Month award
Re: how to open a file (example .txt) using VC++??
thanx andreas, u help me alot.
however, the output takes the value of date and also the next string..
example, output: 2005-04-26 time:18:3
and please if can u explain a bit how the code works so that i can expand it for other variables.
thanx!