|
Thanks. Yes, this is info I want
|
|
|
|
|
Hi all. Im trying to write a simple function that reads from a file thats already in the same folder as the program. However, its not reading the file, but it detects the file.
Here's what i have thus far.
<br />
#include <fstream><br />
using namespace std;<br />
<br />
int main(){<br />
char buffer[100];<br />
ifstream file("file.txt",ios::in);<br />
file.read(buffer, 100);<br />
if(file.fail()){<br />
printf("File didnt open.\n");<br />
}<br />
else{<br />
printf("File opened.\n");<br />
}<br />
system("pause");<br />
return 0;<br />
}<br />
The code compiles fine. But its just not reading. I've searched through my notes and the web and cant find anything that explains what im missing. So if anyone could help out i'd appreciate it. Thanx in advance!
|
|
|
|
|
Actually your code works fine on my system. (I used VC++ 6, on Win2K).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
How can be tested the fact that a file is opened by another application, without trying to delete it or other trivial way?
In VC6 please...
36. When you surround an army, leave an outlet free.
...
Do not press a desperate foe too hard.
SUN-TZU - Art of War
|
|
|
|
|
This probably qualifies as a trivial way, but I suppose you could use CreateFile() to try to
open the file for exclusive access (dwShareMode param == 0) and the dwCreationDisposition set to
OPEN_EXISTING.
I think that will fail if the file is open.
Mark
|
|
|
|
|
I use the _access function - straight C and works every time.
"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
|
|
|
|
|
You can use _access to tell if the file is open by another process?
I think you gave a technically accurate response to a question which was not asked...
|
|
|
|
|
You can use access to test the open/closed status of a file - it doesn't matter what process opened it. Wasn't that the question?
Here's some code directly out of msdn...
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
if( (_access( "crt_ACCESS.C", 0 )) != -1 )
{
printf_s( "File crt_ACCESS.C exists.\n" );
if( (_access( "crt_ACCESS.C", 2 )) == -1 )
printf_s( "File crt_ACCESS.C does not have write permission.\n" );
}
}
"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
|
|
|
|
|
// This example uses _access to check the file named
// crt_ACCESS.C to see if it exists and if writing is allowed.
The question was testing if a file is already open?
A file opened for shared access will mess _access up perhaps?
*EDIT* Actually I messed up my own reply. _access has nothing to do with open sharing but with
the file attributes. Therefore I respectfully disagree with your solution
Mark
|
|
|
|
|
That was my point exactly.
Usually, the CRT functions are an encpasulation of the Win32 API, and since I had already loaded the CRT source code onto my systen, I went and looked at what _access does, and it only relies upon GetFileAttributes, which has nothing to do with determining if a file is open or not.
Your solution with the CreateFile call is closer to what is required.
|
|
|
|
|
He didn't specify the conditions, he just asked a question, and I gave him a possible solution. If you have another suggestion, give it to the OP, not me. I'm not here to argue theory and what-if's.
"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
|
|
|
|
|
See my reply to Blake. Again, I just provided a possible solution. It's up to the OP to pick the best one for his requirements.
"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
|
|
|
|
|
Right I get it. But Blake asked questioned how _access could determine if a
file was open or not. I do too. I'm not arguing - I don't know sh*t. I do like to learn new
methods though.
|
|
|
|
|
Hello,
I just wanted to know how to burn Data/Audio CDs with Visual C++ and Windows...
Are there any libraries to accomplish this task?
With best regards,
Ben
|
|
|
|
|
See if the Image Mastering API (IMAPI) is of any help.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
If you need to see a code for write to CD I think we have examples on the codeproject(nero)
|
|
|
|
|
Hello,
How do I initialize a global 2D array in C++?
First, I declared the 2D array outside of the main function.
int data[32][5];
Then in the main function, I initialized the array:
data[32][5] = {{0,0,0,0,0},
{0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1},
{0,0,1,1,0}, ......................(rest of the integers here)..........
..................................................................................
{1,1,1,1,1}};
The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line.
However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' .
What is the syntax error?
|
|
|
|
|
Put a backslash at the end of each line. This will have the compiler ignore the backslash and the newline character treating the next line as part of the previous line.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
Can you not do something like:
int data[32][5] = {{0,0,0,0,0},
{0,0,0,0,1},
{0,0,0,1,0},
{0,0,0,1,1},
...
{0,0,1,0,0},
{0,0,1,0,1},
{0,0,1,1,0},
{1,1,1,1,1}};
void main( void )
{
...
}
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
You can only initialize an array with { } at the point where it is declared.
|
|
|
|
|
My 5 for seeing the obvious where I failed to...
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
I'm trying ot compile a function that I developed and successfully compiled in Visual c++ 5.0 as well as on Unix. I need to compile it in a 16 bit DOS environment using sockets to interface it with an old Cobol program. I get the error:
error C2065: 'MAKEWORD' : undeclared identifier
on the following code:
#ifdef WIN
WORD wVersionRequested;
int err; // Stuff for WSA functions
WSADATA wsaData; // Stuff for WSA functions
wVersionRequested=MAKEWORD(1,1);
#endif
I have the following includes:
#ifdef WIN
#include <stdio.h> // Needed for printf()
#include <string.h> // Needed for memcpy() and strcpy()
#include <windows.h>
#include <winsock.h>
#include <stdlib.h>
#endif
Any idea why I get this error on code that compiled on other platforms ?
|
|
|
|
|
#include Windef.h<br />
#include Windows.h
jnhemley wrote: I need to compile it in a 16 bit DOS environment using sockets to interface it with an old Cobol program.
I would like to shake your hand. No, really.
/ravi
|
|
|
|
|
Believe me, I didn't know what I was getting myself into when I started this.
|
|
|
|
|
The includes are the following:
include stdio.h
include string.h
include windows.h
include winsock.h
include stdlib.h
I stripped out some of the characters because they didn't print on the forum for my previous message.
|
|
|
|
|