10 Replies - 3934 Views - Last Post: 20 May 2009 - 10:25 AM Rate Topic: -----

#1 prabh   User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 383
  • Joined: 27-December 08

How To Read Ascii 5 byte string from a file in c++

Post icon  Posted 20 May 2009 - 09:43 AM

i have file and it ascii data i want to read a single 5 byte ascii string from string
and its offset or position in the string
actually it is binary file and c++ doestn't have byte arrays i have whole data from file in a string
Is This A Good Question/Topic? 0
  • +

Replies To: How To Read Ascii 5 byte string from a file in c++

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 09:50 AM

5 bytes is 5 characters, whether or not the file is binary is ultimately irrelevant:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	fstream theFile;
	theFile.open("test.txt", fstream::in, ios::binary);
	string placeHolder = "";
	theFile >> placeHolder;
	cout << placeHolder << endl;
	theFile.close();
}



My test file simply had 5 characters: "5char". Your description is very vague, but at the most basic level that is how it works. You can implement an eof() loop if you need other functionality.
Was This Post Helpful? 0
  • +
  • -

#3 prabh   User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 383
  • Joined: 27-December 08

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 09:59 AM

i want position of tat string .,its offset
and thanks for reply
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 10:00 AM

What, its position in the file? Well, in the above example it starts at the beginning of the file (0) and goes to 4 (the 5th character).

Use seekg and tellg to get exact positions.
Was This Post Helpful? 0
  • +
  • -

#5 prabh   User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 383
  • Joined: 27-December 08

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 10:03 AM

i am reading whole file in that string let tempdata
i want its position that string tempdata
that word is my name
the file has that word 19 times
Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 10:07 AM

Quote

Use seekg and tellg to get exact positions.


Inside your loop.
Was This Post Helpful? 0
  • +
  • -

#7 prabh   User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 383
  • Joined: 27-December 08

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 10:13 AM

View PostKYA, on 20 May, 2009 - 09:07 AM, said:

Quote

Use seekg and tellg to get exact positions.


Inside your loop.

my loop run like this
[code]
int star=5,end;
for(int i=0;i<19;i++)
{ end=tempdata.find(Prabh,start)
//array to hold values of offsets
star=end+5
}
Was This Post Helpful? 0
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 10:16 AM

Code tags!

your example raises more questions then it answers. Is find() a function you made yourself? Prabh should be in quotes. Where is start defined? Is star a typo?... so on and so forth...

This post has been edited by KYA: 20 May 2009 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#9 prabh   User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 383
  • Joined: 27-December 08

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 10:20 AM

View PostKYA, on 20 May, 2009 - 09:16 AM, said:

Code tags!

your example raises more questions then it answers. Is find() a function you made yourself? Prabh should be in quotes. Where is start defined? Is star a typo?... so on and so forth...

string.h
has a lsting find
it returns the offset of string prabh and
string Prabh="Prabh"

Was This Post Helpful? 0
  • +
  • -

#10 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 10:23 AM

OK, sounds like you got it down pat. So what's the question/problem?


Not following common coding conventions confuses people. Why is your data variable uppercase? That is why i thought you missed quotes, as it should be lowercase.
Was This Post Helpful? 0
  • +
  • -

#11 prabh   User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 383
  • Joined: 27-December 08

Re: How To Read Ascii 5 byte string from a file in c++

Posted 20 May 2009 - 10:25 AM

View PostKYA, on 20 May, 2009 - 09:23 AM, said:

OK, sounds like you got it down pat. So what's the question/problem?


Not following common coding conventions confuses people. Why is your data variable uppercase? That is why i thought you missed quotes, as it should be lowercase.

find always returns npos whick is -1
and values os star doesnt increase by 5
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1