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

problem reading text file with getlin()

Hello,

I've got trouble reading a text file (event viewer dump) by using the
getline() function...

After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the file...

Thank you to all of you who can help me with this one...
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>

int main () {

int MAX_LENGTH = 10240*10240;
char buffer[1000];
char content[1000];
int result;
int newresult;
int newresult_bis;
int newresult2;
int getpart;
int check;

ifstream notes2 ("c://Data//Utility//Share//Auditing_notes_services//sec_dump.txt");
ofstream putdata("c://Data//Utility//Share//Auditing_notes_services//result_notes.txt",
ios::app);

// lire le fichier sec_dump.txt ligne par ligne - THIS IS THE DUMP
FILE WHERE THE PROGRAM STOPS READING IN THE MIDDLE
while( ! notes2.eof() ) {

char *content2;
content2 = new char[MAX_LENGTH];
notes2.getline (content2,MAX_LENGTH);
_strlwr(content2);

string input2 = content2;

newresult_bis = input2.find("nt authorit");
if(newresult_bis != string::npos) {
continue;
}

ifstream notes ("c://Data//Utility//Share//Auditing_notes_services//source_rep.txt");

// Lire les nom de fichier dans le repertoir ligne par ligne

while (! notes.eof() ) {
notes.getline (buffer,1000);
strcpy(content, buffer);
_strlwr(content);
string input = content;

result = input2.find(input);

newresult = input2.find("object");
string first = input2.substr(0, newresult);

newresult2 = input2.find("dom206");
string first2 = first.substr(newresult2, 14);

// check pour ne pas ecrire de doublons

if(result != string::npos) {

FILE * pFile;
long lSize;
char * buffer4;

pFile = fopen (
"c://Data//Utility//Share//Auditing_notes_services//result_notes.txt"
, "rb" );
if (pFile==NULL) exit (1);

// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);

// allocate memory to contain the whole file.
buffer4 = (char*) malloc (lSize);
if (buffer4 == NULL) exit (2);

// copy the file into the buffer.
fread (buffer4,1,lSize,pFile);
string temporary = buffer4;

/*** the whole file is loaded in the buffer. ***/

string whole = first2 + ";" + input;

check = temporary.find(whole);

if((check == -1) && (first2 != "dom206\\adminis")) {

putdata << first << input << endl;

}

}

}

}
}
Jul 19 '05 #1
1 7004
fabrice wrote:
Hello,

I've got trouble reading a text file (event viewer dump) by using the
getline() function...

After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the file...

Thank you to all of you who can help me with this one...
Please note that this group is for discussion of the *standard C++
language*. If you want to use old, ARM-style C++ you might have to go
somewhere else for help. But first you should consider updating to
modern C++.


#include <iostream.h>
#include <fstream.h>
Neither of these exist in standard C++. Standard C++ uses <iostream> and
<fstream>
#include <stdlib.h>
#include <stdio.h>
These are deprecated in standard C++. The replacements are <cstdlib> and
<cstdio>. That said, I still use these versions because my compiler
incorrectly places names from the <c*> headers in the global namespace,
therefore I have to use the deprecated headers if I want my code to be
standard and still compile. :/

Mixing C & C++ I/O might be a bad idea, also.
#include <string>
Mixing old & new C++ headers could lead to problems.

int main () {

int MAX_LENGTH = 10240*10240;
Did you mean:

const int MAX_LENGTH = 10240*10240;

?

The result of that multiplication (104,857,600) cannot portably fit in
an int object (int may only be able to store values up to 32,767).
Better change it to

long MAX_LENGTH = 10240L * 10240L;

Personally, I would advise against the use of all-uppercase here. That
is traditionally used for macros (which in turn were traditionally used
for constants). This served as a warning to the person using the
identifier, since macros can be dangerous. const objects do not suffer
from the problems associated with macros, therefore this warning is
unnecessary.
char buffer[1000];
char content[1000];
I'd strongly recommend replacing these with std::strings.
int result;
int newresult;
int newresult_bis;
int newresult2;
int getpart;
int check;

ifstream notes2 ("c://Data//Utility//Share//Auditing_notes_services//sec_dump.txt");
ofstream putdata("c://Data//Utility//Share//Auditing_notes_services//result_notes.txt",
ios::app);
In standard C++, the names are std::ifstream and std::ofstream. If you
decide to update you'll need to either use these fully-qualified names
or add appropriate 'using' statements. For example, you could do this at
the top of the file, immediately after the #includes:

using namespace std;

Also, the strings you are passing in to identify the files are highly
suspect. (Note the doubled forward-slashes. If those were back-slashes
instead, it would sort of make sense.) I don't know if those are legal
pathnames on Windows, and you probably shouldn't use full pathnames
anyway (if you expect the program to work on a computer other than yours).

// lire le fichier sec_dump.txt ligne par ligne - THIS IS THE DUMP
FILE WHERE THE PROGRAM STOPS READING IN THE MIDDLE
while( ! notes2.eof() ) {
Two serious problems here. First, you didn't bother to check whether the
file opened correctly.

Second, reading input this way in C++ is almost always a mistake. The
eof flag will only be set AFTER a failed attempt to read PAST THE END of
the file. Therefore, you may arrive at this test when your file has no
data left, but the test will still succeed, and the loop will still
execute, attempting to read and use data from a file that has no data.

char *content2;
content2 = new char[MAX_LENGTH];
Why are you using 'new' here?

char contents2[MAX_LENGTH];

should work just fine. Even better, use

std::string contents2;


notes2.getline (content2,MAX_LENGTH);
If you change to std::strings, you can do

std::getline(notes2, contents2);

here, which is much easier and automatically makes contents2 the right size.
_strlwr(content2);
This is not a standard function. If it's one of yours, then it's
probably invading the implementation's namespace. Identifiers beginning
with '_' followed by a lowercase letter are reserved for the
implementation in the global namespace.

If this function is part of the implementation then it's fine, but not
portable.

string input2 = content2;


It's called 'std::string'. You need either the fully-qualified name or
an appropriate 'using' statement.
That's about all I can be bothered to look at right now. General advise:

* Use modern C++. Lose the char* and char[] strings, use std::strings
instead.

* Don't cram so much into a single function - it complicates code and
breeds bugs. Shoot for functions that take up no more than one or two
screens full of text. Try breaking this up into 2 or 3 functions.

* Don't mix C and C++ I/O. It's messy and unnecessary.

* Fix your pathnames (better yet, get rid of them).

* When posting here, try to leave out all the non-standard stuff. We
can't help you with it anyway.

* When posting here, minimize the amount of code you post. Give us the
smallest complete program that demonstrates the problem you are having.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2

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

Similar topics

2
by: ohaya | last post by:
Hi, I'm a real newbie, but have been asked to try to fix a problem in one of our JSP pages that is suppose to read in a text file and display it. From my testing thus far, it appears this page...
14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
4
by: Nina | last post by:
Hi everyone, Do you know why the following code only read certain number of lines of text file, but not the entire file? Dim sr As StreamReader Dim str As String Dim al As ArrayList = New...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
1
by: JM | last post by:
Hi, I have been trying to read a file, using "StreamReader" to pass the info to an string that is: ....dim stream as new FileStream(file,...) dim sr as new StreamReader(stream) dim data as...
3
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label =...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.