473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.tx t",
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_L ENGTH);
_strlwr(content 2);

string input2 = content2;

newresult_bis = input2.find("nt authorit");
if(newresult_bi s != 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(inp ut);

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

newresult2 = input2.find("do m206");
string first2 = first.substr(ne wresult2, 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.tx t"
, "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,lSiz e,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\\admini s")) {

putdata << first << input << endl;

}

}

}

}
}
Jul 19 '05 #1
1 7053
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.tx t",
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_L ENGTH);
If you change to std::strings, you can do

std::getline(no tes2, contents2);

here, which is much easier and automatically makes contents2 the right size.
_strlwr(content 2);
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
12986
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 is somehow hanging when relatively large file is used. My original intent was to try to just add a check for file size, and error out somehow if the file was "too" large, but in looking at the
14
4566
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 recent entries. When I read line using StreamReader object it starts from the top, so looping though lines and keeping track of the line is not helpful. Is there anyway to start reading from the bottom or if anyone could suggest some other...
8
18249
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 "char" with an ASCII code in 0..127 I've searched the ANSI C++ standard, the internet and various text books, but can't see how to workaround this one. I've tried wchar_t and wstring without success. But rather than spending lots of time on...
7
7770
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 strReturn += input + vbCrLf input = sr.Read
4
1537
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 ArrayList Do str = sr.ReadLine
5
8953
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 headers). I used ODBC in my VB.NET program to read that spreadsheet into a dataset, to make it easy to manipulate. The code I use to read it is as the bottom of this posting.
4
12805
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 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
1
2225
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 string = sr.ReadToEnd... My problem is that in the file (is a text file) there is some data like:
3
3180
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 = 'aeff5' state 0 = 'eee' state 1 = 'ffffff' . .and so on...
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9174
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6634
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.