Hi,
I need help on a problem, as described below. I am reading a file
"input.txt"which has data like this:
abc def gh izk lmnopq rst uvwxyz
I am using fstream object to read the file and writing into a dynamic
array. My problem is that the array shows extra z and probably because
of this further processing gives run time error in borland compiler.
Can you please tell me, if the problem is related to handling end-of
line , how do i do it.
here is the piece of code I am using:
char ch;
char * input;
input = new char[size]; // size is determined at runtime
memset(input, 0, sizeof(input));
ifstream infile("input.txt", ios::in);
if(!infile) cerr << "Error opening file" << endl;
while(!infile.eof()){
infile.get(ch);
input[i] = ch;
i++;
}
cout << input << endl;
infile.close();
as you can see, the output shows extra 'z'
abc def gh izk lmnopq rst uvwxyzz
Thanks for any help!
regards!
dbuser 8 5209
dbuser wrote: Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz
I am using fstream object to read the file and writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland compiler. Can you please tell me, if the problem is related to handling end-of line , how do i do it. here is the piece of code I am using: char ch; char * input; input = new char[size]; // size is determined at runtime memset(input, 0, sizeof(input)); ifstream infile("input.txt", ios::in); if(!infile) cerr << "Error opening file" << endl; while(!infile.eof()){ infile.get(ch); input[i] = ch; i++; } cout << input << endl; infile.close();
as you can see, the output shows extra 'z' abc def gh izk lmnopq rst uvwxyzz
Thanks for any help! regards! dbuser
The problem is that your end of file handling is wrong.
infile.eof() is true is the *last* operation failed because of end of
file, it is not true because the *next* opeation will fail because of
end of file. So you are going round you loop one to many times.
It is simply unbelievable how many newbies manage to get this wrong. I
don't know where they are getting their information from. Did you read
this in a book, or did you just think it must be right?
Here is how you should write your loop
while(infile.get(ch)){
input[i] = ch;
i++;
}
Now the extra z will disappear.
john
dbuser wrote: Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz
I am using fstream object to read the file and writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland compiler. Can you please tell me, if the problem is related to handling end-of line , how do i do it. here is the piece of code I am using: char ch; char * input; input = new char[size]; // size is determined at runtime memset(input, 0, sizeof(input)); ifstream infile("input.txt", ios::in); if(!infile) cerr << "Error opening file" << endl; while(!infile.eof()){
This is your problem. The test for eof() is only meaningful after you
try to get() something. This loop should be written as
while (infile.get(ch)){
input[i] = ch;
++i;
}
You may want to consider reading the file in bigger chunks. Reading it
one character at a time is slooow.
Jacques.
dbuser schrieb: char ch; char * input; input = new char[size]; // size is determined at runtime memset(input, 0, sizeof(input));
^^^^^^^^^^^^^
This is a bad idea. The size of the character array input is pointing to
is "size" and not sizeof(input). sizeof(input) is equal to sizeof(char*).
ifstream infile("input.txt", ios::in); if(!infile) cerr << "Error opening file" << endl; while(!infile.eof()){ infile.get(ch); input[i] = ch; i++; }
I would do it here instead:
input[i] = '\0';
cout << input << endl; infile.close();
Thomas
Hi John,
Thanks for such a quick response. the extra z disappeared , but it
added a junk in the array , I am printing (see the last character.
abc def gh izk lmnopq rst uvwxyz☻
somehow, the file pointer is reading endof line and fills garbage. what
am I missing here?
since I am using this array data to manipulate later, this causes run
time error.
Thanks again ,
dbuser
dbuser wrote: Hi John, Thanks for such a quick response. the extra z disappeared , but it added a junk in the array , I am printing (see the last character. abc def gh izk lmnopq rst uvwxyz☻
somehow, the file pointer is reading endof line and fills garbage. what am I missing here? since I am using this array data to manipulate later, this causes run time error. Thanks again , dbuser
Thomas has answered this. You aren't NUL-terminating your string properly.
Jacques.
Hi Thomas,
Thanks ! Null terminating the string resolved this garbage problem.I
have couple of more questions:
1) I need to find the no. of characters in the input file so that I can
determine the size of the dynamic array. right now, I am using C method
_lseek to find beginning and end value and calculating the size.
Can I do this using C++ methods seekg(ios::beg) and seekg(ios::end)
this does not return a long value , whats the efficient way of finding
size in C++?
2) I am calling a scramble function which fills another dynamic array
with scrambled values, at the end, I need to move user input key into
that array. like moving a null terminating string, I am trying to move
the key value but it is not working.. is this doable.
The other bad idea I have is to write entire array into a file and then
move that key value at end of file , but I am getting run time error
when i try to open the filestream object .
Please help !
Thnaks !
Hi Thomas,
Great, the input[i] = '\0' removed the last unwanted character in the
dynamic array. your suggestion greatly appreciated.
can I ask you another question here... after filling this array, I am
calling a swap function and a scramble function to manupulate the
string... I may not be efficient because I am making another dynamic
array and filling scrambled data.
After I am done, I need to insert a user entered key ( a single digit
number) at the end of the array.
c[j] = x; // x is integer
does not work.
I tried to open a file and write this array data into file so that I
can write end character but then ofstream object creation statement
--- ofstream outfile("name.txt", ios::ate);
gives me run time error. here is the code :
void scramble( char *w, int key)
{
int x = key;
int len = strlen(w);
len = (len + len/x);
char s[] = "@#$";
int n = strlen(s);
int i,j,k,m=0;
static char * c;
c = new char[len];
memset(c, 0, sizeof(c));
if (c == NULL) exit(1);
for (i=0,j=0,k=0; i<len; i++){
if(k<x) {
c[j++] = w[i];k++;
}
else {
if(m<n)
c[j++] = s[m++];
else {
c[j++] = s[0]; m = 1;}
c[j++]=w[i]; k=1; }
}
c[j] = x;
cout << "c is " << c << endl;
//write scramble into a file
cout << "I am starting writefile" << endl;
ofstream outfile("name.txt", ios::ate);
cout << "I m after opening writefile" << endl;
if (!outfile) {
cerr << "file could not be opened " << endl;
exit(1);
} else cout << "file created successfully" << endl;
for (int i=0; i< strlen(c); i++) {
outfile<<c[i];
}
outfile.close();
Thanks a lot for help !
dbuser
dbuser schrieb: Hi Thomas,
Hi again,
After I am done, I need to insert a user entered key ( a single digit number) at the end of the array. c[j] = x; // x is integer does not work.
Of course not. Characters are represented as ASCII code in memory (or
similar on other platforms).
If you want the characters '0', '1', ... write this:
c[j] = '0'+x; // assumes that 0 <= x <= 9
I tried to open a file and write this array data into file so that I can write end character but then ofstream object creation statement --- ofstream outfile("name.txt", ios::ate); gives me run time error.
Try ios::ate|ios::out as second parameter. ios::ate does not imply ios::out.
here is the code : void scramble( char *w, int key) { int x = key; int len = strlen(w); len = (len + len/x); char s[] = "@#$"; int n = strlen(s); int i,j,k,m=0; static char * c;
Why static?
Here are the errors:
c = new char[len]; memset(c, 0, sizeof(c));
if (c == NULL) exit(1);
First, don't check c for NULL after using it. That really makes no
sense. Then, you don't have to check c for being null-pointer, since
operator new does not return NULL, it throws an exception instead.
You also need to reserve space for the null-terminator:
c = new char[len+1];
Then again, as I said in the other posting, the array c points to does
not have size sizeof(c). It's size is len.
Write this:
memset(c, 0, len); or better omit it, you don't need it.
Instead, terminate the string with '\0' when done, just as in the other
function.
for (i=0,j=0,k=0; i<len; i++){ if(k<x) { c[j++] = w[i];k++; } else { if(m<n) c[j++] = s[m++]; else { c[j++] = s[0]; m = 1;} c[j++]=w[i]; k=1; } }
What are you doing here anyway? What do you scramble the string for?
c[j] = x;
See above.
cout << "c is " << c << endl; //write scramble into a file cout << "I am starting writefile" << endl; ofstream outfile("name.txt", ios::ate); cout << "I m after opening writefile" << endl; if (!outfile) { cerr << "file could not be opened " << endl; exit(1);
Don't forget to free the memory c is pointing to bevor exiting.
} else cout << "file created successfully" << endl; for (int i=0; i< strlen(c); i++) { outfile<<c[i]; }
Why do you write the characters one at a time? outfile.close();
Again, don't forget to delete c!
Thomas This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matt |
last post by:
I always heard people saying IIS ASP front end, and MS-SQL back end. ASP is
for server side programming and dynamic content generation, how could it is
called front end? Because I thought it is...
|
by: mitchchristensen |
last post by:
I have a transaction log that tracks issues from a call center. Each
time an issue is assigned to someone else, closed, etc. I get a time
stamp. I have these time stamps for the beginning of an...
|
by: Jeff Pritchard |
last post by:
Some time ago I am sure I came across something that said this was possible,
though it doesn't seem to work.
A client wants to replace an Access back-end with SQL Server tables. We have
tried...
|
by: John Baker |
last post by:
Hi:
I have an application that has a back end and a front end. I need to add a table which is
identical to one in the back end, and then use it for a temporary holing place form some
records. I...
|
by: Murt |
last post by:
on my inbox below i want the loop to quit if the user enters End,
It does not function as written,
any ideas?
thanks
----------------------
Dim inputString As String
|
by: oktave |
last post by:
Hi,
Anybody would like to tell me ther defference between
Application.Exit() and
End? I can use
End to end my application no matter how
many forms and codes after the End
statement. But since...
|
by: Bob |
last post by:
My app shows a modal MDI parent form, and creates a timer that ticks every
ten minutes to boot unresposive users at 3 in the morning. Below is a
simplified working example with no time check. My...
|
by: shapala |
last post by:
Hi all!
Can anybody help me with this problem:
I have a database splited with to front-end back end application. At front end I have a form who contains combo box. This combo has row source to a...
|
by: Michael |
last post by:
Hi,
I am confused about end() in the following code. end() does not point
to the 10th element? Why? Does end() means like '\0' in a char x ?
Thanks in advance,
Michael
#include <iostream>...
|
by: JohnC |
last post by:
RE: Access 2003
My application has been split and the front end runs on client PCs with the
back end on a LAN file server. Also I have an updater routine that copies
an updated client from the...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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: 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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |