473,394 Members | 1,715 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,394 software developers and data experts.

File Handling

ben
Hi guyz,
Say a line "abcdef..." is written to a file.
The file is created like this :
ofstream fout;
fout.open("temp.dat");
fout<<str;//str is string with contents like abcd....
fout.close();

Now i open the file for reading using

ifstream fin;
fin.open("temp.dat");

The pointer will be by default positioned to the 0th byte of the
file(beginning of the file).

Now i want to find the letter 't' or say 'w' in that string.

The problem is i wont be knowing what is the size of the
string and where it ends.(The string is not null terminated.There might
be whitespace at the end).

I tried out all possible functions like fin.peek(),fin.ignore(MAX,'\n')
and so on but in vein.

Can anyone suggest a way out?(I can't use commands like find)
How do i find out what is stored in nth byte of the file?

bye,
ben

Jul 23 '05 #1
3 1201

"ben" <bi*******@rediffmail.com> skrev i meddelandet
news:11**********************@f14g2000cwb.googlegr oups.com...
Now i want to find the letter 't' or say 'w' in that string.

The problem is i wont be knowing what is the size of the
string and where it ends.(The string is not null terminated.There might
be whitespace at the end).


How about this:

ifstream fin("temp.dat");
std::string line;
std::getline(fin,line); // Reads until widen('\n') or until end of file.
CallYourMethodOfChoiceHereToFindTheCharacterTOrWha tever(line);

Regards,
Peter Jansson
http://www.jansson.net/
Jul 23 '05 #2
ben
well my problem got solved like this:
ifstream fin("temp.dat")
char c;
while(c !=' * ')
{
fin>>c;
}
..
..
Thanks anyway.

Jul 23 '05 #3
ben wrote:
well my problem got solved like this:
ifstream fin("temp.dat")
char c;
while(c !=' * ')
{
fin>>c;
}
.
.
Thanks anyway.


There are a few things wrong with that code. For one, c is left uninitialized
and then later compared to a value. Also, your character literal ' * ' has 3
characters in it. You're also not checking for EOF. This is probably better
(maybe):

ifstream fin("temp.dat");
char c;
while(fin >> c) {
if(c == '*')
break;
}
Jul 23 '05 #4

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

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
5
by: John Douglass | last post by:
I'm fairly new to doing involved file i/o and I came across something weird with a program I'm writing (modifying MIDI data, if it makes any difference). With some input files, when I modify data...
8
by: Gabe Moothart | last post by:
Hi, I'm writing a windows service which interacts with a separate process. Basically, it calls a process which creates a file, and then my service reads that file. The problem is, the external...
14
by: Al Smith | last post by:
I need help in implementing proper error handling. I am trying to upload a file based on the sample code below. The code works well except if the file selected is too big. I do know about the...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
5
by: Digital Puer | last post by:
I fixed a bug today that went against my intuition. I am on Linux. I had a class that fopen'ed some files. When I called delete on these objects, I expected that the files would be closed...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
5
by: kailashchandra | last post by:
I am trying to upload a file in php,but it gives me error msg please Help me? My Code is like below:- i have one php file named upload.php and i have another html file named upload.html and...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.