|
As far as you open the file in read mode, you can not write anything in that file ...
|
|
|
|
|
What are you trying to do, exactly?
If you need to manipulate the content of a file the usual way is:
- Read the file content into memory.
- modify it
- Write the modified content to another file (say "output").
- delete the original file and rename the "output" file you produced with the original filename.
Veni, vidi, vici.
|
|
|
|
|
You cannot do it that way; a file is merely a stream of bytes and overwriting something with a shorter value will leave garbage in the stream. You need to follow something similar to my response here[^], but using text instead of PDF files and changing lines rather than pages. If you want to delete a line then merely read it and ignore it.
Programming is work, it isn't finger painting. Luc Pattyn
|
|
|
|
|
Just to amplify CPalini's answer - if you're doing line oriented I/O then open the file for reading, open another for writing then process each line from the input file putting the results in the output file.
Another thing you could try is have fixed length records in the file. Then to read a record all you have to do is fseek to (record_size * index_of_record) . To replace it you just write to the same place and to erase it you just mark the entry as empty somehow. This means you only need a single file open but makes the logic a bit more complicated.
Cheers,
Ash
|
|
|
|
|
HI guys,
How to read data from a file and save that data
|
|
|
|
|
See here[^], paying particular attention to CreateFile() , ReadFile() and WriteFile() .
[edit]
On second thoughts you may be better to start here[^] for pure C.
[/edit]
Programming is work, it isn't finger painting. Luc Pattyn
|
|
|
|
|
Is there a multiprocessor implementation of qsort and bsearch out there? My app. is mostly in C but some bits have got C++ and STL depending on what libraries are needed. We compile with VS2008 so openMP is available.
|
|
|
|
|
An article I read recently on sorting in parallel: http://www.drdobbs.com/blogs/parallel/232600358?pgno=1[^]
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
hi,
i
int COMportNum= 3;
portstream_fd COMstream;
portstream_fd pstream;
char COMportPrefix[10] = "COM";
char COMportName[256];
sprintf(COMportName, "%s%d", COMportPrefix, COMportNum);
strng = CString(COMportName);
strng.Format(_T("%0.9s"), strng);
SetDlgItemText(IDC_STAT, strng);
set_baud_rate(BaudRate);
COMstream = open_host_port(COMportName);
if ( COMstream == PORT_NOT_OPENED )
{
stw.Format(_T("%08X"), GetLastError());
SetDlgItemText(IDC_PTUPORT, stw);
}
else
SetDlgItemText(IDC_PTUPORT, (LPCTSTR) L"Ok");
can anyone please help me with this error.
Regards
Jawad
|
|
|
|
|
Hi there,
I don't want to seem pedantic, though I will point out that this is not actually system error 0x0000007B, but rather the error-code of the last system function that failed. A system error is one generally regarded as having been generated by Windows itself (rather than just a 'helper' function)
Something that you may choose to do which certainly clarifies the error is to display a description of the error.
Have a look over here, for a method by which the code 0x7B may be turned into a text message.
|
|
|
|
|
The error message is (you can check it in WinError.h )
What library are you using?
Veni, vidi, vici.
|
|
|
|
|
basically i have the console program running fine (means having source file and header files). than i wanted to build GUI for this using MFC in vs2008. now i don't understand how to open serial port in MFC. code below shows how to open serial port in console programming using cmd prompt.
COMportName[0] = ' ';
while (COMportName[0] == ' ') {
printf("\nEnter the %s port number the PTU is attached to: ", COMportPrefix);
scanf("%d", &COMportNum);
printf("You selected %s%d. Is this OK? (enter 'y' or 'n'): ", COMportPrefix, COMportNum);
tmpChar = 'f';
while ( (tmpChar != 'y') && (tmpChar != 'n') )
tmpChar = ((char) tolower(getchar()));
if ( tmpChar == 'y' )
sprintf(COMportName, "%s%d", COMportPrefix, COMportNum);
}
tmpChar = 'f';
while (tmpChar != 'y') {
printf("\nEnter the baud rate the ptu is communicate at (default: 9600): ");
scanf("%d", &BaudRate);
printf("You selected %d. Is this OK? (enter 'y' or 'n'): ", BaudRate);
tmpChar = 'f';
while ( (tmpChar != 'y') && (tmpChar != 'n') )
tmpChar = ((char) tolower(getchar()));
}
set_baud_rate(BaudRate);
COMstream = open_host_port(COMportName);
if ( COMstream == PORT_NOT_OPENED )
{ printf("\nSerial Port setup error.\n");
goto abnormal_exit; }
printf("\nSerial port %s initialized\n", COMportName);
|
|
|
|
|
jawadali477 wrote: COMstream = open_host_port(COMportName);
I would use the debugger to watch COMportName content in both console and MFC applications.
Veni, vidi, vici.
|
|
|
|
|
console program is running fine. problem is in MFC program. can you tell how to implement the console program, as mentioned, in MFC??
|
|
|
|
|
What a mix up of different naming conventions!
|
|
|
|
|
As CPallini pointed out
CPallini wrote: The error message is (you can check it in WinError.h )
Are you sure COM3 exists on your machine?
Did you try the format \\.\COM3 instead?
What happens in set_baud_rate() and in open_host_port()? You have to open the COM-Port before changing the baudrate
Cou can find some more hints on serial communication here:
Communications Resources[^]
[Edit]corrected spelling of memeber name [/Edit]
modified 15-May-12 5:16am.
|
|
|
|
|
Andy411 wrote: JPalini
Veni, vidi, vici.
|
|
|
|
|
Oooouuuuuuch
Sorry, I just corrected it. Sometimes I don't have any control on what my fingers are doing with the keyboard
|
|
|
|
|
yes COM3 is available and i have tried \\.COM3 and \\\\.\\COM3 both. basically i'm developing GUI via MFC in vs2008 for my console program. part of the code for opening serial port in console program is given below. i can't understand how to do it in MFC.
COMportName[0] = ' ';
while (COMportName[0] == ' ') {
printf("\nEnter the %s port number the PTU is attached to: ", COMportPrefix);
scanf("%d", &COMportNum);
printf("You selected %s%d. Is this OK? (enter 'y' or 'n'): ", COMportPrefix, COMportNum);
tmpChar = 'f';
while ( (tmpChar != 'y') && (tmpChar != 'n') )
tmpChar = ((char) tolower(getchar()));
if ( tmpChar == 'y' )
sprintf(COMportName, "%s%d", COMportPrefix, COMportNum);
}
tmpChar = 'f';
while (tmpChar != 'y') {
printf("\nEnter the baud rate the ptu is communicate at (default: 9600): ");
scanf("%d", &BaudRate);
printf("You selected %d. Is this OK? (enter 'y' or 'n'): ", BaudRate);
tmpChar = 'f';
while ( (tmpChar != 'y') && (tmpChar != 'n') )
tmpChar = ((char) tolower(getchar()));
}
set_baud_rate(BaudRate);
COMstream = open_host_port(COMportName);
if ( COMstream == PORT_NOT_OPENED )
{ printf("\nSerial Port setup error.\n");
goto abnormal_exit; }
printf("\nSerial port %s initialized\n", COMportName);
|
|
|
|
|
Does the console programm run?
And what's the code inside open_host_port()?
Did you step through the code of open_host_port() in the debugger? Examine every return code of Win32 API-calls and check GetLastError().
What happens, if you just do a call to the CreateFile-function to open the COM-port, just like in this article
Creating a Serial communication on Win32[^]
I dont't think that this is an MFC problem,
|
|
|
|
|
Andy411 wrote: I dont't think that this is an MFC problem, I agree.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
jawadali477 wrote: char COMportPrefix[10] = "COM";
char COMportName[256];
sprintf(COMportName, "%s%d", COMportPrefix, COMportNum);
strng = CString(COMportName);
strng.Format(_T("%0.9s"), strng);
SetDlgItemText(IDC_STAT, strng); I'm thinking you could eliminate some overhead here. Try:
strng.Format(_T("COM%d"), COMportNum); Note that this likely has nothing to do with why open_host_port() is not opening the port.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
I do not recognize the open_host_port function, but it does not really matter. On some systems you need to add a ':' after COMx. Try sprintf(COMportName, "%s%d:", COMportPrefix, COMportNum); to get COM3:
Cheers,
Henryk
|
|
|
|
|
i did what you suggested and now the error comes out to be 00000002 (i.e The system cannot find the file specified). and the outcome of
SetDlgItemText(IDC_STAT, strng);
is "COM3:".
|
|
|
|