473,416 Members | 1,546 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,416 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 7020
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 =...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

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.