LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to truncate a file?

Hello,

I want to truncate a file without having to copy it in a temporary file.

I try to put the EOF character in my file or something like that without succes.
I found the SetEndOfFile() function in the basicsdk library but it doesn't seem to work, it returns 0.

Do you have any idea?

Here is my code with my try in comment:

            TrouveTemps = False;
            FichierTemp = fopen(CheminTemp,"r+");
            while(fgets (LigneTemp, sizeof(LigneTemp)-1, FichierTemp)!=NULL && !TrouveTemps )
                {
                pFinHeure = strstr(LigneTemp,"\t");
                if(pFinHeure != NULL)
                    {
                    *pFinHeure = 0;
                    DureeEnreg = ChaineToHeure(LigneTemp);
                    if(DureeTotal<DureeEnreg)
                        TrouveTemps = True;
                    }
                }
            j=4;
            //fwrite (&j, 1, 1, FichierTemp);
            //fputc(4,FichierTemp);
            //putchar(EOF);
            j = SetEndOfFile(FichierTemp);
            fclose(FichierTemp);

Thanks

Yop!
DanY           
Message 1 of 9
(6,273 Views)
HI!

   I've checked, but found nothing.... putting EOF character does not solve, and you don't have a "truncate file from here" function.  The only way to truncate a file is deleting all data. 

   So, I think you're forced to use a temporary file.....

   I'm sorry I didn't find a solution, if someone else has one....

   Bye!

graziano
0 Kudos
Message 2 of 9
(6,263 Views)
Thanks,

But what about that SetEndOfFile() function from windows?

Has someone already used it with success?

Yop!
DanY
0 Kudos
Message 3 of 9
(6,255 Views)

Well, SetEndOfFile does behave correctly on files opened with windows API functions. Look at this function that selects a file and truncates it to 256 bytes.

 

int CVICALLBACK TruncateFile (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 char file[MAX_PATHNAME_LEN], msg[512];
 DWORD error = 0;
 HANDLE fH = NULL;

 if (event != EVENT_COMMIT) return 0;

 if (FileSelectPopup ("", "*.*", "", "File to truncate",
   VAL_SELECT_BUTTON, 0, 0, 1, 0, file) == VAL_NO_FILE_SELECTED)
  return 0;

 //HANDLE CreateFile (
 // LPCTSTR lpFileName,                         // file name
 // DWORD dwDesiredAccess,                      // access mode
 // DWORD dwShareMode,                          // share mode
 // LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
 // DWORD dwCreationDisposition,                // how to create
 // DWORD dwFlagsAndAttributes,                 // file attributes
 // HANDLE hTemplateFile                        // handle to template file
 //);
 fH = CreateFile (file, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 if (fH == INVALID_HANDLE_VALUE) {
  //DWORD GetLastError (VOID);
  error = GetLastError ();
  goto Error;
 }

 //DWORD SetFilePointer (
 // HANDLE hFile,                // handle to file
 // LONG lDistanceToMove,        // bytes to move pointer
 // PLONG lpDistanceToMoveHigh,  // bytes to move pointer
 // DWORD dwMoveMethod           // starting point
 //);
 if (SetFilePointer (fH, 0xff, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
  error = GetLastError ();
  goto Error;
 }

 //BOOL SetEndOfFile (
 // HANDLE hFile   // handle to file
 //);
 if (!SetEndOfFile (fH)) {
  error = GetLastError ();
 }

Error:
 if (fH != INVALID_HANDLE_VALUE) CloseHandle(fH);

 if (error) {
  memset (msg, 0, 512);
  //DWORD FormatMessage(
  // DWORD dwFlags,      // source and processing options
  // LPCVOID lpSource,   // message source
  // DWORD dwMessageId,  // message identifier
  // DWORD dwLanguageId, // language identifier
  // LPTSTR lpBuffer,    // message buffer
  // DWORD nSize,        // maximum size of message buffer
  // va_list *Arguments  // array of message inserts
  //);
  FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, msg, 511, NULL);
  if (!strlen (msg)) sprintf (msg, "Error %d", error);
  MessagePopup ("Error", msg);
 }
 return 0;
}

Message Edited by Roberto Bozzolo on 04-07-2006 03:36 PM



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 4 of 9
(6,251 Views)
Great!
  I learnt something more!  Smiley Very Happy

   Anyway, this is a windows API function, I always try to manage with ANSI-C functions, and, when possible, LabWindows' libraries functions......

   By the way, why does open() function is not  ANSI-C standard??????????? Smiley Mad  (I'm asking to NI people....).

   Bye! And have a great HolyWeek!

graziano

0 Kudos
Message 5 of 9
(6,242 Views)
Thank you very much, i'll see if I change of library

Have a nice WE.

Yop!
DanY
0 Kudos
Message 6 of 9
(6,236 Views)
Graziano,

That's a question that you'll have to ask the ANSI folks, not NI Smiley Tongue

My understanding is that those low-level IO functions are part of the POSIX standard. CVI still includes them in our C library for convenience, since they're fairly common. However, they're not part of the ANSI specification.

Luis
NI
0 Kudos
Message 7 of 9
(6,227 Views)
Hi LuisG,
   Thanks for your interest!

Bye!

graziano

0 Kudos
Message 8 of 9
(6,220 Views)

I was quite happy at your post.

It works well.

I open the file with function OpenFile as a CVI function.

In order to use the CreateFile Function I needed to

set the parameter FILE SHARE_WRITE in the CreateFile function.

Then the file was created by a CVI function and closed and reopend

with the CreateFile function.

 

Thanks a lot for your post.

 

Yours sincerely

 

Othmar Widmer

 

 

0 Kudos
Message 9 of 9
(4,610 Views)