|
V. wrote:
fscanf(readfile, "%s", line);
...
while(line.Find("\n") < 0);
If line is a CString object, it cannot be used as the third parameter to fscanf() .
Assuming you are wanting to read lines from the file, there are at least two methods that make more sense. The first is to use "%[^\n]" instead of "%s" . This will read everything up to the '\n' character. The second is to create a CStdioFile object and use its ReadString() method.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
tnx, this seems to work fine now
"If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
|
|
|
|
|
The problem lies in
fscanf(readfile, "%s", line);
You cannot pass a CString variable as the parameter to fscanf. In this case it needs a char *
You will have to use a char array, or perhaps line.GetBufferSerLength(?) (see MSDN)
In either case you'll have to have some idea of the expected line length and cope with the possibility of buffer overflow. Think about a complete redesign.
The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).
|
|
|
|
|
Hi,
I have a problem to find a good pattern for Initializing some data once on demand, in a thread-safe manner.
My first (not thread-safe) stab at it would be:
void GetSomething(int index)
{
static CSomethingTable table;
return table[index];
}
I can ry to put a critsect around the table intialization, but I regulary end up with the same problem.
Any takers?
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
What's wrong about wrapping the thing with a critical section. AFAIK this should work:
static CRITICAL_SECTION cs;
static bool initialize_cs()
{
InitializeCriticalSection(&cs);
return true;
}
static bool cs_initialized=initialize_cs();
...
void GetSomething(int index){
EnterCriticalSection(&cs);
static CSomethingTable table;
LeaveCriticalSection(&cs);
return table[index];
} You might want to embellish it by using RAII and stuff, but IMHO this is correct (modulo well known problems with the order of initialization of translation units.)
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
The frequent scenario I have is the SomethingTable being thread safe itself, only the initializaiton is not.
This works if I make the critical section global static (i.e. I don't care in which order CS'es are initialized, they are initialized before the function is called).
However, I have a CS initialized for every Singleton, even If I never use it. I hoped for something better, but maybe I am desiring to much
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
|
ringle looks good - the sample code even has th destruciton in it
Now I only need a "Mutex" with zero-cost-initializaiton :-/
(I wanted to avoid the "init Critical Section even if we never need the singleton" implementaiton - see also my reply above)
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
Peter, before you go and adopt the double check solution, please be aware that this idiom, appealing as it looks, is fundamentally broken. The most thorough exposition of the problem is given in Bacon et al. paper The "Double-Checked Locking is Broken" Declaration[^]. This issue has been devoted literally thousands of posts on comp.lang.c++ and comp.lang.c++.moderated, with threading experts rejecting its validity and lots of enthusiastics proposing seemingly correct formulations: one could call this problem the Fermat's Last Theorem of multithreaded programming, if you know what I mean (except that the theorem is true )
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Interesting post Joaquín, thanks. I quickly scanned the referenced article. Out of interest do you have any references to techniques that do work correctly?
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
I don't have a copy-and-paste snippet (I wish I did), sorry; seems you have to look for "memory barriers", which ensure the logical ordering of read/writes th DCL pattern relies on. These memory barriers are not portable
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
No problem, I don't actually need code for this (yet). I would have thought that the InterlockedIncrement() et.all. functions would come it to play here.
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
I know, it needs an memory barrier, or a volatile, or... something similar. I was looking for somethng reusable, platform specific, and efficient for larger libraries. I just wanted to check with you guys before following the ideas of my own (which don't work without a dreaded while(cond) Sleep(1)
The solutions I see don't scale well - especially the perfect ones bring in just to much code for a small library, and the acceptable ones are simply not "good enough".
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
In "Modern C++ Design" Alexandrescu says that Double-Checked Locking will work on most processors with most compilers, but you need to check!
Chapter 6 is all about Singletons and worth a read.
I tried to avoid Sleep() except for the real thing.;)
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
ringle looks good - the sample code even has destruction in it, which I didn't dare to ask
Now I only need a "Mutex" with zero-cost-initialization :-/
(That was my initial motivation. I want to avoid the "init Critical Section even if we never need the singleton" implementaiton - see also my reply above)
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
Hi!, i hope you guys can help me out.
1.- to get the size of a file (i dont use mfc), i use something like:
HANDLE hfile=CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, 0, NULL);
if(hfile)
{
DWORD fsize=GetFileSize(hfile, NULL);
CloseHandle(hfile);
}
is there a better way? like one that doesnt require using W32 API calls? (to keep the code portable)
2.- i now want to read from a file, using ifstream class, but the ifstream::read()function takes an int as the ammount of data to read! what if i want to read say 20MB of data? wont it overflow? . and also (ok ok its 3 questions), is there a better way to read binary (not text) data from a file? i could use the W32 APIs like ReadFile(), ReadFileEx(), etc. but i would like to keep my code as portable and object oriented as possible
thanks!
|
|
|
|
|
1. To get the size of a file, take a look at the _stat function, and the st_size member of the structure it populates.
2. Will an integer overflow? Depends on the compiler. The ANSI standard basically says that an integer can be whatever size the compiler manufacturer wants it to be. In early versions of Visual C++, targetting Windows 3, it was 16 bits. In a specialist compiler for an embedded system, it might only be 8 bits. In Visual C++ 6, its 32 bits. So provided you are using a modern version of Visual C++, you shouldn't have a problem.
3. Not a huge expert on non-Windows C++ development, but I would imagine that anything that uses the STL would be portable.
|
|
|
|
|
|
another way to get the file size (and it's portable too) is to use the ftell function:
FILE *f = fopen("foo.dat","rb");
long lFileSize = 0;
if (f != NULL)
{
fseek(f,0,SEEK_END);
lFileSize = ftell(f);
fclose(f);
}
|
|
|
|
|
I have setup two PCs for multilink connections using two modems each. One PC as dialer and another for receiving call. After the PCs got connected I used "RasEnumConnections" function to get the details of the connections. But I always get only one "RASCONN" structure. If only modem 1 is connected I get the value 1 for the subentry. If only modem 2 got connected I get the value 2 for the subentry. But if both modem got connected I only got 1 structure returned from "RasEnumConnections" and subentry=1. Why don't I get two RASCONN structure? How do I get all the RASCONN for all subentries ?
|
|
|
|
|
Hi !
My application is using an Access database through ODBC.
Now, if I want to make it run on a computer which doesn't have MS Office installed, it doesn't work because there is no ODBC Access driver.
Do you know if it is possible (and how) to install only an ODBC driver for Access and if this is freely installable ?
Thanks for your help !
Jerome
|
|
|
|
|
Hi also
You should install MDAC 2.7 and your problem is resolved. You can get it from MS for free. See ya Kurt
Fast Prototyping
|
|
|
|
|
Additional to what Kurt has said, you do not need to install MDAC on Windows XP or Windows 2003 as they come pre-installed with the OS.
IIRC Windows XP has at least 2.7 and Windows 2003 has at least 2.8
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
I've being trying to get CString directly to float, but maybe I need to go through another class like 'const char *' first.
This is my problem code:
void CTest2Dlg::OnSelchangeList1()
{
int Stock; //Stock item quantity
int ListIndex; //Number of Items in list
int StrLen; //Length if string in list
double TAG; //Stock item cost inc. tax
float RRP; //Stock item rrp inc tax
float GP; //gross profit
CString WorkingStr; //complet list item string
CString TempStr; //extract of WorkingStr
CListBox * List1=(CListBox *)GetDlgItem(IDC_LIST1);
ListIndex = List1->GetCurSel(); //get selected item 0 based
List1->GetText(ListIndex,WorkingStr); //get WorkingStr from list
TempStr = WorkingStr.Mid(47,3); //Extract stock from WorkingStr
SetDlgItemText(IDC_STATIC4,TempStr); //Display stock on dialog
TempStr = WorkingStr.Mid(54,8); //Extract TAG Ex tax from WorkingStr
// problem code
sscanf(TempStr, "%f", TAG); //supposed to assign float TAG value of //CString TempStr. Not working
// end problem code
TAG = TAG * 1.1; //add 10% tax to TAG
TempStr.Format(_T("$%f"),TAG); //Assign TAG to CString
SetDlgItemText(IDC_STATIC5,TempStr);//Display TAG on Dialog
TempStr = WorkingStr.Mid(62,8); //Extract RRP ex tax from workingstr
SetDlgItemText(IDC_STATIC6,TempStr);//display RRP ex tax on dialog
//finish code when sscanf or other like function works
}
Thats the code. Can anyone show me how to get the value of TempStr into TAG? It shouldn't matter, but I'm using MS Embedded Visual C++ writing for Pocket PC
Thanks for any help you can give me!
Jochum Wittebrood
|
|
|
|
|
if you wonna convert from CString to float you can use following
CString sFloatString=_T("5.45");
TCHAR *tmpChar;
float fAValue = _tcstod( sFloatString.GetBuffer(0), &tmpChar );
That should pretty much do the trick
Greetz,
Davy
|
|
|
|