|
jmarkey wrote: What the heck is a stream?
It's what's used with cout .
jmarkey wrote: How can I
access the acc_time value? (acc_time is a double)
What's wrong with t.acc_time ?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
hiiiiiiiiiiii..........
Q:
I have in my programe three files: SCANNER.CPP,tst_scanner.cpp and scaner.h
i need read and write from text file which is glopal. how i can do it?
the strange things if i put all programm in one file it is working as good.but whene i use three files it is have error(not define the glopal text file in some file).
i am using many wayes to solve this probleme(i define the glopal text file out of main or enter and enter of getT() or outer of it or the top of any file is .cpp or using 'extern' and....)but i did not get the solve.
pleeeeeeeeeeeeeeeeeeeeez i neeeeeeeeed to know how i do it? if i can opening the text file to be glopal in constructor of my class that good for mee.
NOTE: i need to use this hider of function: Tokens Scanner::getT()
I am writing in SCANNER.CPP this lines code:
#include<iostream>
#include"scanner.h"
#include<fstream>
static char ch;
using namespace std;
extern fstream myfile1;
int lineo=1;
Tokens Scanner::getT()
{
.
.
.
}
-------------and in main file tst_scanner.cpp i write this
#include<iostream>
#include"scanner.h"
#include<fstream>
using namespace std;
int line=1;
static char ch;
//extern fstream myfile1;
int main()
{
//extern fstream myfile1;
fstream myfile2, myfile1; //static fstream myfile1,myfile2;
Scanner S1;
Tokens T1;
myfile1.open( "C:\\fcode.txt", ios::in | ios::out );
myfile2.open( "C:\\fscan.txt", ios::in | ios::out|ios::app );
myfile1.get(ch);
myfile2<<"Tokens_Type"<<"\t\t\t"<<"Line Number"<<"\t\t"<<"Lexmes"<<endl;
do
="" {
="" t1="S1.getT();
" switch(t1.tokentype)
.
.
.
}while
=""
---------------------and="" in="" scaner.h="" i="" write="" this:
typedef="" enum="" {="" tok_ident,tok_il,kw_else,kw_if,kw_int,kw_return,kw_void,kw_while,tok_semicolon,
tok_plus,tok_star,tok_slash,tok_eq,tok_gt,tok_gt_eq,tok_lt,tok_lt_eq,tok_not_eq,tok_assign,tok_lbrace,
tok_rbrace,tok_lbracket,tok_rbracket,tok_lpar,tok_rpar,tok_comma,tok_minus,end,error}tokentypes;
class="" tokens
{
="" friend="" class="" scanner;
="" public:
="" tokentypes="" tokentype="" ;
=""
="" char*="" lexeme;
="" int="" newval;
};
class="" scanner
{
public:
="" scanner();="" constructor
="" tokens="" gett();="" -----------="" return="" object="" of="" (tokens).
="" -----------and="" tokentype,lexeme="" and="" line
};
=""
i="" have="" some="" time="" this="" error="" or="" no="" but="" the="" file="" not="" read="" scanner.cpp
--------------------configuration:="" test_scanner="" -="" win32="" debug--------------------
linking...
scanner.obj="" :="" lnk2001:="" unresolved="" external="" symbol="" "class="" std::basic_fstream<char,struct="" std::char_traits<char=""> > myfile1" (?myfile1@@3V?$basic_fstream@DU?$char_traits@D@std@@@std@@A)
Debug/test_scanner.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
test_scanner.exe - 2 error(s), 0 warning(s)
--------------------------------------------------
thanks for help!!!
|
|
|
|
|
extern means you define it somewhere else. I don't see where you are doing that.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
// extern in my code means how many i try to make text file glopal.
i try to put extern in difrent place and file but it is not work
in scsnner.cpp file in getT() function the text file that what i open it in main() not define.
can you tell me how and where put extern? and can you tell what the reason of this ERROR:
--------------------Configuration: test_scanner - Win32 Debug--------------------<br />
Linking...<br />
Scanner.obj : error LNK2001: unresolved external symbol "class std::basic_fstream > myfile1" (?myfile1@@3V?$basic_fstream@DU?$char_traits@D@std@@@std@@A)<br />
Debug/test_scanner.exe : fatal error LNK1120: 1 unresolved externals<br />
Error executing link.exe.<br />
<br />
test_scanner.exe - 2 error(s), 0 warning(s)
thanks.....
|
|
|
|
|
You get an unresolved external because you've not declared it anywhere, you've just put extern in various places, which tells the compiler that it's declared somewhere ( but not here ), but it never is.
Either way, what you want to do is nasty. You should wrap your file access in a static class, not make it a global like this.
|
|
|
|
|
immmmmmm ok..
can you plz explain that by using my code above?
|
|
|
|
|
Well, it's really simple. You need to declare that variable somewhere, without the extern keyword. Otherwise, the object does not exist.
And it's nasty because you have a global flying around, when you'd do better to have it all encapsulated in a class.
|
|
|
|
|
Christian Graus wrote: Well, it's really simple. You need to declare that variable somewhere, without the extern keyword. Otherwise, the object does not exist.
And it's nasty because you have a global flying around, when you'd do better to have it all encapsulated in a class.
can you gif me exampel of it ALL encapsulated in a class
and is a new class or in my exist class?
and when i define in scanner.h
Class{fstream myfile;};
i have error that not define fstream. then pleeeeez gif me exampel using my code above or any one
i am feeling taird of this problem
|
|
|
|
|
top7ob wrote: i have error that not define fstream.
Then #include fstream and put using std::fstream.
I'd put the class in a different header, and include it in the cpp files of the classes that use it. I'd make it static, and consider opening and closing the file every time.
|
|
|
|
|
i define new class named fileclass in new header
but my question is: where can i define the object of class
is in the main or out of main.
my problem is i need open file in main and read it from getT().
main() and getT in defrent files.
can you tell me is new class for file correct?
if not correct,correct it to me.
and where and how i define the object of text file is in 1,2,3,or4
-----sccaner.h
:((class fileclass<br />
{<br />
fstrem myfile1;<br />
fileclass::fileclass(){<br />
myfile.open(.........);<br />
}<br />
}<br />
----testscanner.cpp<br />
(<code>1)<br />
int main()<br />
{<br />
(2<br />
}
--------
file scanner.cpp
(3)<br />
getT()<br />
{<br />
(4)<br />
}
|
|
|
|
|
top7ob wrote: but my question is: where can i define the object of class
is in the main or out of main.
I said to make it a static class. That means the variables and methods are static, and you don't need to define an instance anywhere.
|
|
|
|
|
hi all..
i've done a small tool which load strings from a text file and puts it to a cstringarray. the stringarray is created on program startup and deleted on close. All worked ok for text files under 1MB, but if i load a 7 MB text file..and check in task manager...i can see about 100MB of RAM consumption.
there is some way to reduce this Ram abuse to a more reasonable value?
thanx in advance
|
|
|
|
|
I wish i knew
Just as a suggestion, try using a few smaller arrays, rather than one big one, who knows it just might help
|
|
|
|
|
Only 1428% overhead? That's not bad.
|
|
|
|
|
I had a similar problem the other week. Mine occured after manipulating and resizing large arrays. Try adding
SetProcessWorkingSetSize(GetCurrentProcess(),-1,-1);
to your code. Add it somewhere after the constructor has done all it's memory intensive work.
|
|
|
|
|
very thanx to all.
i used this code and the ram decreased to 1 MB...but obviously the virtual memory remains to about 100 MB.
The problem about doing a setsize is that the file is loaded by user input, and so i should scan the text file one time and count the number of words to set array size. And this kind of scanning on a 7MB file is slow.
|
|
|
|
|
ayol12122 wrote: there is some way to reduce this Ram abuse to a more reasonable value?
Im not entirely sure this is related or not as I use std::vector for arrays, but MSDN
CStringArray Class [^] has the following little tidbit that might be of interest to you.
MSDN wrote:
Before using an array, use SetSize to establish its size and allocate memory for it. If you do not use SetSize, adding elements to your array causes it to be frequently reallocated and copied. Frequent reallocation and copying are inefficient and can fragment memory.
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
|
|
|
|
|
ayol12122 wrote: ...and check in task manager...i can see about 100MB of RAM consumption.
Which is not necessarily how much RAM your process is using. The number reported by TM is actually the size of the address space. Even when you free memory, that number does not necessarily go back down. The is the way Windows' memory manager works.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
In a service that I wrote I am creating a process (using the CreateProcess API call) that is actually a bat file. When the service is signaled by the SCM that it should stop, I want to terminate the batch file and any child processes it has started. Is this possible? From looking at the props that SysInternals Process Explorer show, it would appear that any process launched by the batch file doesn't have a parent. Is there a way around this?
|
|
|
|
|
Jim Crafton wrote: Is there a way around this?
I don't know about "around" but why not through? Don't use createprocess on the "batch file", rather create the actuall processes and then your process will be the parent and it's all good.
led mike
|
|
|
|
|
Jim Crafton wrote: ...any process launched by the batch file doesn't have a parent.
Wouldn't cmd.exe be the parent?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Apparently not according to Process Explorer. I would have thought the same thing and was really surprised to see that. Perhaps it's because I'm running this all through services?
|
|
|
|
|
Instead of using a batch file, run the apps from your own app. That way, you can call CreateProcess on all of them and stop them when you want to because you have the hwnd and processid.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Is that why you are an "outlaw" programmer because you plagiarize posts?
led mike
|
|
|
|
|
Calling TerminateProcess is a bad idea. The API is only indented to be called in extreme circumstances. When called the .EXE and all its threads are killed on the spot without getting a chance to do any cleanup. None of the DLLs get the DLL_PROCESS_DETACH or DLL_THREAD_DETACH notifications.
Steve
|
|
|
|