473,405 Members | 2,310 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,405 software developers and data experts.

File Processing

Hi,
I have a file with the format
//////////
{blkid:= 10000}
dfd dfd
dfdfdfd
dfd dfd
{blkid:= 10001}
dfd fddd
gdfd dd
dfdd
ere
///////

I want to set '{' as a delimiter and read everything between into a
std::string.
I checked out getline but it expects a "size", in my case I dont know
how big
will the buffer be so how should I solve this problem. I have attached
a sample code which compiles but does not give me the string I expect.
Can you help.
Thanks.
Kapil
// semantics_of_data.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
ifstream sample("sample.dat",ios::in);
ifstream outfile("temp.dat",ios::out);
char buf[10];
std::string sample_buf;

if(!sample || !outfile)
{
exit(0);
}

while(!sample.eof())
{
sample.getline(const_cast<char*>(temp_buf.c_str()) ,30,'{');
outfile << temp_buf;

}

gets(buf);

return 0;
}
Jul 19 '05 #1
2 6299
Kapil Khosla <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...
Hi,
I have a file with the format
//////////
{blkid:= 10000}
dfd dfd
dfdfdfd
dfd dfd
{blkid:= 10001}
dfd fddd
gdfd dd
dfdd
ere
///////

I want to set '{' as a delimiter and read everything between into a
std::string.
I checked out getline but it expects a "size", in my case I dont know
how big
will the buffer be so how should I solve this problem.
Don't use the member function 'getline()', use the
'free' function 'getline()' declared by <string>.
This function allows the specification of a delimiter,
and also stores the input directly into a 'std::string'
object, no messing about with arrays.
I have attached
a sample code which compiles
I see at least a few things that I think should prevent it
from compiling. See below.
but does not give me the string I expect.
Can you help.
Thanks.
Kapil
// semantics_of_data.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
This is a nonstandard, Microsoft-specific header.
Please omit such from code posted here. It's
certainly not necessary for what you're asking about.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
Eh? More 'Microsoft-isms?'. The language discussed
here is ISO standard C++, for which the *only* allowed
name for the entry-point function is 'main()'.

Also _TCHAR is some platform-specific type, not allowed
by the language definition (unless it resolves to the
C++ type 'char').

Why declare these parameters at all anyway? You're
not using them.
{
ifstream sample("sample.dat",ios::in);
ifstream outfile("temp.dat",ios::out);
Note that it's redundant to specifiy 'ios::in'
for type 'ifstream', since that 'mode' is its default.

More important, you're trying to use an 'ifstream'
object for output. Didn't your compiler complain?
char buf[10];
What's this for?
std::string sample_buf;

if(!sample || !outfile)
Why not check the stream states immediately after opening,
instead of creating a couple of objects first?
{
exit(0);
Don't you think it would be more 'polite' to at least
inform the user *why* the program abruptly halts?
}

while(!sample.eof())
You're misusing 'eof()' here. It does *not* return
true until *after* an attempt to read past end of file.
It does not 'predict' that the next read will encounter
end of file.
{
sample.getline(const_cast<char*>(temp_buf.c_str()) ,30,'{');
Wow, what malicious abuse of casting!! This is evidence
that one should *really know* what one is doing before
using a cast. 'std::string's member function 'c_str()'
returns an array of *const* characters. Your cast does
*not* change this fact. It's like driving your car
toward a brick wall, and closing your eyes in the belief
that if you don't see it, a collision will not occur.

What you have above is 'undefined behavior'.

Also, there is no 'temp_buf' defined at this scope.
I simply don't believe your above statement that
your code compiles.
outfile << temp_buf;
Again, I see no definition for 'temp_buf'. Also,
you have defined 'outfile' to be type 'ifstream'.
There are no '<<' operators for type 'ifstream'.
This is another statement the compiler should have
diagnosed.

}

gets(buf);
Ack! The Mortal Sin in C and C++ programming. *Never*,
and I mean *never* use 'gets()'. Ever. Pretend it does
not exist. There is absolutely *no way* to use it safely.

Even if there were, you failed to provide its prototype
from <cstdio> or <stdio.h>

By, the way, why are you calling 'gets()' at all? You're
not using what it stores.

If you need to 'freeze' a graphical window containing your
program so it won't 'close', just use:

cin.get();

return 0;
}


Finally, to completely answer your question:

std::getline(sample, sample_buf, '{');

Input is stored directly into a 'std::string' object,
no 'size' parameter needed (the 'std::string' will adjust
its size as needed automatically), no dangerous
arrays, no need to abuse casting, and a delimiter
can be specified (defaults to '\n' if not).

If the 'real' code you have does compile, copy
and paste it here, don't retype it, so these
kinds of 'spurious' errors don't waste your time
and ours.

A perusal of the 'C++ FAQ' should help you greatly:
http://www.parashift.com/c++-faq-lite/

-Mike

Jul 19 '05 #2

"Kapil Khosla" <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...
Hi,
I have a file with the format
//////////
{blkid:= 10000}
dfd dfd
dfdfdfd
dfd dfd
{blkid:= 10001}
dfd fddd
gdfd dd
dfdd
ere
///////

I want to set '{' as a delimiter and read everything between into a
std::string.
I checked out getline but it expects a "size", in my case I dont know
how big
will the buffer be so how should I solve this problem.


You looked at the wrong getline

getline(my_file, my_string, '}');

john
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sure | last post by:
I my php script I call a perl script that perl script will create a log file From the php script I want to read the log file and write the output to the browser. This operation has to carried out...
18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
5
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
8
by: Imran | last post by:
hello all, I have to parse a text file and get some value in that. text file content is as follows. ####TEXT FILE CONTENT STARTS HERE ##### /start first 0x1234 AC /end
6
by: Glenn Owens | last post by:
OK I'm on a steep learning curve with XML et.al. and need some advice. I'm writing a B2B front door for a new application. I have multiple data suppliers all sending various formats of flat...
5
by: Geoff Pennington | last post by:
My VB.Net app reads an Excel file, processes it one row at a time, and when processing is complete writes the row to a database. The typical file will have several thousand rows and may take a...
4
by: jesuraj | last post by:
Hi, how can i read input from a data file which contains binary or hex values. I have to use the exact binary data for further processing.only limited number of bits are taken form the file(64...
5
by: B. Williams | last post by:
I need some assistance with random access file processing. I have a function that I would like to change from sequential file processing to random access. Thanks in advance. void...
20
by: Johan | last post by:
How can I include one XML file into another XML file (on the client side, in Firefox)? I think XInclude is just what I need, but Firefox doesn't support it:...
6
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.