LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Help!!!File Handling in LabwWindows/CVI 7.0

Hello everyone,
              I am a beginner in LabWindowsCVI7.0.I want to copy the contents of a file say abc.c to a buffer.When I use the function fgetc() and take each character one by one a runtime error occurs.Error is that Dereference of out-of-bounds pointer:1 byte past end of array.Also last two lines of the file are not copied.I wrote the code as:
     while(!feof (input_file))        //input_file is the file handler of abc.c
         {
           while ((c=fgetc(input_file))!=0x0a)
          
             Data[i++]=c;      //error is on this line.
    //Here the statements related to my application is written and these statements are applicable to each line of the file..

           } 
I don't Know how to handle the situation.My sir told me to complete it by the next 3 days.Later I tried the function fgets() instead of fgetc() so that I can read a line as such.This time also I didn't get the correct result. Hope u all understood my problem.As I am in the beginning stage I don't know how to deal with it.Expecting help from all.
                                                                                                                            vava
0 Kudos
Message 1 of 9
(4,934 Views)

The error you are receiving means that you have dimensioned a too little array for the file you are reading. How have you dimensioned the array? Raising its size should easily solve your problems.

As far as I can understand the inner loop is used to read the file contents discarding the end-of-line character: you could also accomplish it by using OpenFile and ReadLine instead of fopen and fget(x): in my opinion these functions are of much easy use and ReadLine automatically discards end-of-line character(s).



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?
0 Kudos
Message 2 of 9
(4,931 Views)
hello,
      Thank u.So U are saying that instead of using fopen and fgetc i must use OpenFile and ReadLine.I will try it and will reply the result soon.
 
0 Kudos
Message 3 of 9
(4,927 Views)
Hello,
                As u said I used OpenFile and ReadLine but how can I go thru the whole file.using this Readline.when I used fgetc() and fopen I used feof()  to parse the whole file.But in this case(when I use ReadLine) what to do.I have no idea.Later instead of using ReadLine I used ReadFile function.But now when  I try to print the copied file I get the Error  message as "Attempt To read beyond end of string".Please give an idea of how to copy the entire file on to a  buffer  using any function.I want to copy the whole file.Please help me.
 I wrote the code as:
                
char  *Data;
                       inpfile = OpenFile
            (filename,
             VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);               //filename is selected from a combobox
                    GetFileSize (filename, &fsize);
          Data = malloc (fsize);
      ReadFile (inpfile, Data, fsize);
       printf(\n%s",Data);
          
Note If I wish to copy the whole file back from the buffer to another file I must get an exact copy of the original file.In that way I must copy it.         
                                                                                                       Vava
                                                                                                                                  

Message Edited by vava on 12-11-2005 11:25 PM

0 Kudos
Message 4 of 9
(4,891 Views)
Hello,
              Thank u,I succeeded in copying an entire file using ReadFile.But no idea of reading an entire file using ReadLine.Those who read my above message could understand my problem.In the application that uses readLine() I must scan each line for  the occurence of a comma. so in that case i must use readline itself.Code is in my first message.Kindly give me timely advice.
                                                                                                                                 Vava
0 Kudos
Message 5 of 9
(4,884 Views)

Hello vava.

Roberto is correct; you should find it much easier to do what you want using the OpenFile() and ReadLine() functions.

You could try something like this (assuming the file is already open):

bytesRead = ReadLine (handle, buffer, 255);

while (bytesRead >= 0)    // no error (-1), not end-of-file (-2)
    {
    linesRead++;

    if (bytesRead > 0)    // non-blank line
        {
        // Process the current line
        // Try the Scan() function; see the CVI help for examples
        ...
        }

    bytesRead = ReadLine (handle, buffer, 255);
    }

If you want to make a copy of a file, you could try the CVI function CopyFile().

Regards,
Colin.

 

0 Kudos
Message 6 of 9
(4,876 Views)

Hello Collins,

                   Thank u.Can I read the whole file using the loop that u have specified.Can u givea brief explanation of it.If so I can understand it very well.

                                                                                                                           Vava.

0 Kudos
Message 7 of 9
(4,876 Views)

"Can I read the whole file using the loop that u have specified"

 

Yes, you can. The code I posted will do precisely that (with the addition of OpenFile() and CloseFile() calls outside the loop). ReadLine() returns a non-negative value until end-of-file or an error occurs. You can call Scan() within the loop to parse each line of the file.

 

[I apologise if you already know the following]

 

CVI has comprehensive Help and Context Menu systems; you really should become familiar with them. You access Help by pressing the F1 key; you call up the context menu by a right-mouse-click on the item of interest. Alternatively, you can use the Help menu at the top of the main CVI window. For example:

  • right-click on a library function name then click "Recall Function Panel"

  • right-click on a control on the Function Panel to access the Control Help

If you look at the attached image, you will see the type of information that is available.

Regards,
Colin.

 

0 Kudos
Message 8 of 9
(4,832 Views)
Hello Colin,
                           Thanks for ur timely advice.Now I am able to understand the explanation of every functions,
                                                                                                                                            Vava
                                                                                                 
0 Kudos
Message 9 of 9
(4,815 Views)