GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Nov-2008, 19:57
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Pointers and C ++ (use)


i am taking data structure I , but it seems that i did not get the real use of pointers with classes, so i was thinking to create a programm that reads a text file pass it to a string , then a have a pointer char to go through each characters fron that file and convert it (from ascii to the binary code equivalent and write it into another output file) [ My whole goal is to create a decypter for text files at the end i know i may not finish this idea or i may in the wrong direction but at least i want to understnd pointers from my idea so i am doing in two part: the first part will just do what is described above if it works fine than i will move to changing the dycryption method and then without know the decryption formula i will try to find the algorithm ] so i any one can help me it will be greatly appreciated.
  #2  
Old 14-Nov-2008, 21:58
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Pointers and C ++ (use)


Quote:
Originally Posted by zatora
...
Wow, you said all that in one sentence...

If you read in the entire file into memory, than the file size is limited by the amount of remaining available memory in the process space. I would suggest just reading in a single line at a time & encrypt/decrypt just that.

We see an abundance of people who are completely befuddled by implementing all (& then some...) features/functionality at once. Make life simpler for yourself by incrementally building your application:
  • Implement all necessary file I/O first.
  • Once you can read a line at a time, figure out how to encrypt/decrypt it.
  • Then figure out how you will be writing an encrypted file to disk.
  • Lastly, determine how you will read in an encrypted file & decrypt it.
  #3  
Old 15-Nov-2008, 02:17
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Pointers and C ++ (use)


why this code is not working properly ?
CPP / C++ / C Code:
int main()
{
char *ch;
*ch='A';
cout<<static_cast<int>(*ch);
return 0;
}
While this one is perfect ?
CPP / C++ / C Code:
int main()
{
char ch;
ch='A';
cout<<static_cast<int>(ch);
return 0;
}
i was always thinking that *ch is to retreive the value which ch points too
unless we have to have a char variable that has a value first maybe that will be the reason
so i tried this way still not working and that is what i don't get about pointers like i can not tell what this
statements means reall:
CPP / C++ / C Code:
char * ch;
ch=new char[size]; // 
	or
char *ch
ch=newchar[];//
now as i said in my first post which i wanna thank you for the reply
i have a text file that i created with a notepad.txt. I added couple of lines without using any Return button(keyboard)
i made sure there is no space after the last char int his text.
now how to read that char by char and put it into an array(i think my problem is not beeing able to get the right function from the class<fstream>

So my final goal is to read from an unknowing size txt file that print those character into another file.
  #4  
Old 15-Nov-2008, 03:24
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Pointers and C ++ (use)


Quote:
Originally Posted by zatora
i was always thinking that *ch is to retreive the value which ch points too
Yes it is. But in your first code, what does *ch point to? You never loaded the pointer so it points to nowhere-you-want-to-be.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #5  
Old 15-Nov-2008, 03:59
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Pointers and C ++ (use)


ok so how can i assign a char pointer to a string base position;
example :
CPP / C++ / C Code:
string str="this is a test";
char *ch;
// now i wanna use a for loop to browse throuhg the string char by char;
ch=new char[str.length()];
for(int i=0;i<str.length();i++)
{
ch[i]=str[i];
cout<<*ch<<" "<<str[i];
ch++;}
and later on i want my input to be a text file which is an unknowing size
same technique going through char by char and write the output to another
txt file.
  #6  
Old 15-Nov-2008, 05:27
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Pointers and C ++ (use)


Please post complete examples which compile. Posting snippets only makes sense if the full example has previously been posted.
Quote:
Originally Posted by zatora
CPP / C++ / C Code:
ch=new char[str.length()];
Careful, now. Note that the length() member function does the same thing as strlen() -- returns the number of characters found in the string excluding the terminating null. If the statement above is meant to allocate sufficient heapspace for a copy of str's underlying C-style string, it doesn't.
Quote:
ok so how can i assign a char pointer to a string base position;
Look at the c_str() member function:

http://www.cplusplus.com/reference/s...ing/c_str.html
Quote:
CPP / C++ / C Code:
for(int i=0;i<str.length();i++)
{
ch[i]=str[i];
cout<<*ch<<" "<<str[i];
ch++;}
Various comments:
  • Quote:
    CPP / C++ / C Code:
    ch[i]=str[i];
    This statement makes sense in the context that it is presented.
  • Quote:
    CPP / C++ / C Code:
    cout << *ch << " " << str[i];
    This statement doesn't make sense at all. ch is the beginning address of an allocated array, so referencing it to *ch will alway display the same character.
  • Quote:
    CPP / C++ / C Code:
    ch++;
    Coupled with the following:
    Quote:
    CPP / C++ / C Code:
    ch[i] = str[i];
    This combination is a disaster. You are changing/losing the base address of the array.
  • Again, you are not copying the terminating null.
You may be confusing yourself by mixing concepts from the the C++ string class with the underlying C-style string in which it wraps. Using the class only makes sense if the functionality & abstraction helps, but here it seems to be getting in your way. Given that you are ultimately wanting manipulate each character found in the string (I'm guessing...), you may find the syntax simpler by staying with the C-style string functions found in string.h.
  #7  
Old 15-Nov-2008, 06:43
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Pointers and C ++ (use)


Thanks as i get what was u trying to say by void g(int*);i am ok with passing by value and refrence and to point a pointer to var just we use this statement : p=&i; now p points to i and changing *p will alter i automatically;
here is two full code with a text file attched to them i commented what i did not get, i hope this will clear everything thanks forward;
CPP / C++ / C Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void getData(ifstream & input,char & temp,int & asc);

int main()
{
ifstream in;
in.open("sample.txt");
char ch;int temp_ascii;
getData(in,ch,temp_ascii);
in.close();


	return 0;
}
void getData(ifstream & input, char & temp,int & asc)
{

while(!input.eof())
{
	input.get(temp);
	if (temp==' ')
		temp = '32';
		asc=temp;
	cout<<temp<<" "<<asc<<endl;
	
}
}
the second version is through the use of pointers
CPP / C++ / C Code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
	ifstream in;
	ofstream out;
	in.open("sample.txt");
	out.open("output.txt");
	 char *ch;
	 ch=new char[];// here i need to know what this statement does 
	 int ascii;
while(!in.eof())
{
	in.get(*ch);
	ascii=*ch;
	cout<<*ch<<" "<<ascii<<endl;
}
	in.close();
	out.close();
	return 0;
}

now how if i have a text file with a big size will the same technique work ? i forget to say my text file is: "thanks ocicat for the help" i named it sample.txt ;
  #8  
Old 15-Nov-2008, 09:36
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Pointers and C ++ (use)


hi ocicat i see u online can u check my last response if u have a chance thanks
  #9  
Old 15-Nov-2008, 10:17
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Pointers and C ++ (use)


Although I suspect you are simply doing a cut-&-paste potentially from Microsoft's Developer Studio or some other environment which may massage tabs & spaces into coherent indentation, the code posted is nearly incomprehensible.
Quote:
Originally Posted by zatora
CPP / C++ / C Code:
void getData(ifstream & input, char & temp,int & asc)
{

while(!input.eof())
{
	input.get(temp);
	if (temp==' ')
		temp = '32';
		asc=temp;
	cout<<temp<<" "<<asc<<endl;
	
}
}
It is unclear whether you understand that the if-statement found in the above quote is actually interpreted as:
CPP / C++ / C Code:
if (temp==' ')
    temp = '32';
...as opposed to:
CPP / C++ / C Code:
if (temp==' ') {
    temp = '32';
    asc=temp;
}
It is your responsibility to ensure the clarity of your questions & code. Since no one is being paid to answer your questions, if too much work is required to decipher your intent, your questions will soon be ignored.

Quote:
CPP / C++ / C Code:
ch=new char[];// here i need to know what this statement does[
This statement is not syntactically correct. It appears that you are wanting to allocate an array characters, but the number of characters is not being specified.
  #10  
Old 15-Nov-2008, 12:07
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Pointers and C ++ (use)


Hi, In the code posted earlier there is a mistping line i put temp instead of ascii so i am reposting it again
Ocicat you said that you suspecting that i am copying and pasting ok i send the source code that i wrote from scratch, if we all agree that encryption is something that means reading data and changing the dispay of actual datat to a different form that dispaly a different output so that was my project. as i wanted to try it with pointers to char
i made a sample test file where i wrote a string in it saved it with notepad
i iopened it in the main function then i created
i declared a variable int ascii
with a while loop i am reading through my sample text file
the in.get(*ch) takes the first char from the text
if this char is blank the ascii code 32 is assigned
else
the integer ascii to the (*ch) which will put the ascii code of the character in ascii
i am dispaying throught the screen and writng to another text file called output.txt
i am incremeting the pointer ch )char type) to the next char uing ch++;
closing both files

So i did another version instead of using *ch i used just a variable characheter and
it did work and u can try to compile it and see all u have to if u are using VC++ express edition is
to create a dummy text file type whaterver in it and save it then run the program.
*/
CPP / C++ / C Code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
	ifstream in;
	ofstream out;
	in.open("sample.txt");
	out.open("output.txt");
	char *ch;
	int ascii;
	ch=new char[];
		while(!in.eof())
		{
		int i=0;
		in.get(*ch);
			if(*ch==' ')
				ascii=32;
		ascii=*ch;
		out<<*ch<<" ASCII code = "<<ascii<<endl;
		cout<<*ch<<" ASCII code = "<<ascii<<endl;
		ch++;
		}
	
	in.close();
	out.close();

	return 0;
}
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort an array of structures sassy99 C Programming Language 10 15-Feb-2007 08:46
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 12:33
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:47.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.