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
  #11  
Old 15-Nov-2008, 12:04
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 592
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Pointers and C ++ (use)


It does not appear that you are doing any testing because the code does not compile. Once again, the statement:
Quote:
Originally Posted by zatora
CPP / C++ / C Code:
ch=new char[];
...is syntactically incorrect. The code will not compile as presented.

It appears that you want to write all characters into a buffer. The buffer must have a size. For compilation to succeed, the size must be known. Even though you may not know what is the longest line in the file, you have two choices:
  • Create a buffer of sufficient size that it will always be larger than the longest line anticipated.
  • Make your application read through the file twice:
    • Once to determine the longest line.
    • During the second pass, do whatever processing you want to do.
I would recommend that you study the following discussion on istream::getline():

http://www.cplusplus.com/reference/i...m/getline.html

Once you understand the usage of getline(), you can use it in file I/O as:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

int 
main() {
    const int size = 256;
    char buffer[size];
    ifstream fin("sample.txt");

    while (fin.getline(buffer, size)) {
        cout << buffer << endl;
    }

    fin.close();

    return 0;
}
  #12  
Old 16-Nov-2008, 01:06
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 111
zatora will become famous soon enough

Re: Pointers and C ++ (use)


Hi, Ocicat i think i get your point ok i declared a *ch (char type) then this statement is wrong ch=new char[]; cuz *ch will be aa dynamic arry of char without a size but i think may be the express edition vc++ 2005 is taking that as ch=new char[0]; cuz i run the application and it did work and i can just use a char variable not a pointer.
now i checked your code the buffer is reading an amount of [255] the how to empty it and fill it up again where u left.
  #13  
Old 16-Nov-2008, 01:37
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 592
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Pointers and C ++ (use)


Quote:
Originally Posted by zatora
...the express edition vc++ 2005 is taking that as ch=new char[0];
It is not clear what you attempting to say here.

Any space that is allocated at runtime must be non-zero in size. While the following:
CPP / C++ / C Code:
char ch = new char[0];
...may be syntactically allowed, it does not make any practical sense, & no heapspace is being allocated as a result of executing this statement. If you attempt to use such a buffer, I would not use the value in ch with any confidence. In fact, none.
Quote:
...i run the application and it did work and i can just use a char variable not a pointer.
This makes no sense.
Quote:
now i checked your code the buffer is reading an amount of [255] the how to empty it and fill it up again where u left.
Have you compiled & executed the code provided? It will echo the entire contents of sample.txt to stdout. The code reads the file line-by-line into the buffer with the assumption that no line exceeds 255 characters. Your question does not make any sense. If you have modified the code, you will need to post it since it is not clear what you are talking about.

The buffer is repeatedly overwritten with the current line read from the file. Once the line is echoed to stdout, the buffer is overwritten with the next line.

I gather that English is not your first language. Coupled with the fact that you are not comfortable with the concepts involved, I question the quality of the communication. I would recommend that you discuss these matters with your instructor.
 
 

Recent GIDBlogMatch IP in CIDR by gidnetwork

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 07:46
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 11:33
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35

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

All times are GMT -6. The time now is 11:37.


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