Write/read struct in binary file

Hi to all,
I created a struct profile defined as follows:
typedef struct profile {
char name [30];
float power;
double frequency;
struct profile * next;
I can write the structures in a file, but I should do so that when you try to insert a struct, you control the field name and only if there is no other struct with that name,it is inserted in the file .
Please help me !!!
Solved!
Go to Solution.

I tried this way
#include <ansi_c.h>
#include <cvirte.h>
char nuovo[50];
int cmp;
FILE *fp;
struct profilo
char nome[50];
float potenza;
double frequenza;
}p;
int main (int argc, char *argv[])
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
fp = fopen ("c:\\Program Files (x86)\\National Instruments\\CVI81\\ESERCITAZIONI\\scrittura file 2\\profili.txt", "ab+");
 printf("Insert name: \n");
scanf("%s",nuovo);
do
fseek(fp,sizeof(struct profilo),SEEK_SET);
fread(&p,sizeof(struct profilo),1,fp);
//printf("nome: %s\n",nuovo);
//printf("p.nome: %s\n",p.nome);
p.nome==nuovo;
printf("p.nome dopo: %s\n",p.nome);
cmp=strcmp(p.nome,nuovo);
if(cmp=0)
  printf("Already exists\n");
fclose(fp);
else
p.nome==nuovo;
scanf("%f%lf",&p.potenza,&p.frequenza);
}while(!feof(fp));
fclose(fp);
return 0;
but don't work . 

Similar Messages

  • Error when Write/read Cluster in binary file.

    hi all,
    I implemented a code (using the Labview example) to save and read cluster data (see attached file). The write part works properly but when I want to read the file, there is an error : "Error 4 occurred at Read from Binary File in main_save_data_V2.vi; LabVIEW: End of file encountered'.
    BUT the data are correctly open. Why is there such message? any suggestions?
    thank you.
    Cedric
    Attachments:
    main_save_data_V2.vi ‏19 KB

    Instead of predicting how many clusters to read, just set the count in the binary read to -1.  That will read all that it can and I got no error.  Here is all you need in order to read the file.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines
    Attachments:
    Read clusters.png ‏12 KB

  • Reading little endian binary files

    Hi,
    Recently I tried to read from a little endian binary files. Here is the code:
    FileInputStream file_input = new FileInputStream (file);
    DataInputStream data_in = new DataInputStream (file_input );
    while (true) {
    try {
    short i_data = data_in.readShort ();
    catch (EOFException eof) {
    System.out.println ("End of File");
    break;
    System.out.println ( i_data );
    But I am getting wrong values (big-endian). I will be pleased if anyone can help me in this respect.

    Hi,
    Finally, I can do it easily with reading little endian binary files in java. Here is the code:
    FileInputStream file_input = new FileInputStream (file);
    DataInputStream data_in = new DataInputStream (file_input );
    while (true) {
    try {
    int low = data_in.readByte() & 0xff;
    int high = data_in.readByte() & 0xff;
    short i_data = (short) (high << 8 | low) ;
    catch (EOFException eof) {
    System.out.println ("End of File");
    break;
    System.out.println ( i_data );
    }

  • How to read non-java binary file?

    Hello Team,
    I have problem to read non-java binary file.
    Let me explain.... I have one binary file created in vc++ having fix structure. Structure of file is like String,int,int,int.
    Now I have to read this file in my java application. I am failed to identify length of String value of Structure in java.
    Can any body help me to solve this problem?
    Thanks in advance.
    - Pathik

    Thanks for guide me,
    I have try using 0x00. And its working.
    Now I have another problem:
    in file.ext , I have written one record having structure string,int and int.
    I have enter data as "HelloWorld", 100 and 111.
    To read first record from file file.ext, following code I am using:
    try
         FileInputStream fis = new FileInputStream("file.ext");
         DataInputStream in = new DataInputStream(fis);
         char ch;
         while((ch=(char)in.readByte()) != 0x00)
              System.out.print(ch+"");
    System.out.println();
         System.out.println("Integer 1 --> " + in.readInt());
         System.out.println("Integer 2 --> " + in.readInt());
         in.close();
         fis.close();
    catch(Exception ex)
         System.out.println(ex.toString());
    And I am getting following output:
    HelloWorld
    Integer 1 --> 0
    Integer 2 --> 0
    File file.ext is created in vc++
    I am not getting integer data. Plz guide me for this problem.
    Thanks in advance
    - Pathik

  • Error 2 for write/read access to a file. File = /usr/sap/SID/SYS/global/999

    hi
    when i am checking update statistics in  db13  it is not creating any log information, and more over it is creating error in sm21 like
    Error 2 for write/read access to a file. File = /usr/sap/<SID>/SYS/global/999JOBLG/0001X06000603X23459.
    can any one help me.
    regards
    subhani.

    Duplicate post
    Read and Follow the Forum Rules

  • Error when reading back a binary file

    Hello,
    I am writing out a binary file consisting of U32 arrays, packed strings, and clusters. I get error 116 when reading back. As long as I am synchronized on the correct data position why should LV care what I write to the file? In the example I write a simple 16 element array and a time stamp cluster. When reading back the array I get the error. What is the correct way to do mixed structures? Thanks.
    Attachments:
    file write.PNG ‏44 KB

    David,
    You can solve the problem in various ways. The easiest might be to modify the writer VI by wiring a True constant into the "header (F)" input of your first Write File node. That forces LabVIEW to include a four-byte header that specifies the length of the array you are writing.
    When you wire up the reader code the way you did, the length of the array you wire to the "byte stream type " input is irrelevant; by wiring nothing into the "count" input, you are implicitly telling LabVIEW to look for a header to determine the length of the array you are asking it to read. Because you didn't write any header, LabVIEW is interpreting the first four bytes of the array itself as a length. That length doesn't match the actual amount of data present in the datafile, so LabVIEW generates your error.
    An alternative solution would be to leave the writer unchanged and modify the reader VI: wire an I32 constant into "byte stream type" and wire a 16 into "count". The choice depends on what you prefer and whether or not these binary files need to be compatible with some other program or not.
    Another aside: you can dispense with your case that checks whether the datafile already exists. Just change the function input of the Open/Create/Replace to "create or replace" and wire a False constant into its "advisory dialog" input.
    Hope that's clear,
    John

  • Problem in Writing to/Reading from a Binary file placed in a Loop

    Hi
    As you can see in the attahced image, I attempt to write a set of 2*202 data in each iteration of the loop of the loop, yet when I try to read the data (in the second picture) I only get the first (or last, I assume) set of data, up to index 202. I needed to read two set (X, Y) of 402 valuse. So, I am not sure if I am making a  mistake in wrting to the file or reading from it! 
    I really appreciate it if someone could suggest a solution
    Ashakn
     

    You are only reading the first.  In your read, set your number to read to -1.  That will tell the Read Binary File to read all of the data instead just a single set.

  • Write/read image to avi file, VI works incorrectl​y from the second run

    Hi everybody, in my labview I write image from IMAQ USB to avi file and read image from avi file, the VI runs correctly at the first run then after that it goes wrong, I can write/read only one frame and it terminates automatically. Does anybody know why and how to solve this?
    Thanks a lot,
    Hannah

    Without seeing your code I can't comment specifically, but if it runs correctly the first time and then stops be looking for something that is not getting reinitialized correctly. Also what exactly do you have to do to get it to work again? Close and reopen the VI? Restart LV? Restart the computer?
    Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

  • Reading in a binary file and displaying an array correctly

    I am teaching myself Labview 7.1 and what I am trying to do is read in a text file of binary digits 10011001, 11011101 etc and then output these to a series of leds (boolean array) as well as to another array to display what was read in. I can get this to happen when I format the text file as 1,0,0,1,1,0,0,1 but then the array is 1 column by 8 rows (is this a 1by8 array) I want to read this data in as 10011001 displayed on 1 row and then the next row of the array to be 11011101, it displays 11111111 if I go above 3 digits, I know this is just a problem with binary versus decimal numbers, how can I format the input file so it is read in as 8-bit binary?
    Thank you in advance,
    Dan Ziehm

    Read the text file as string, then pass it through "spreadsheet string to array" with "," as delimiter, %b as format and an U8 array as type. I made a quick demo, see attached code image.
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    binaries.gif ‏45 KB

  • Follow/read a growing binary file

    Hello,
    Application space:
    1 - I have an application that has created and opened a binary file, and is is writing data to it
    on a continual basis.
    2 - Using another program written in Labview, I need to open this file for reading, get all the data available
    (for the given file size at that instant) and update a strip chart/graph. The program also needs
    to continue to follow that growing file and update the chart as new data is written to it by the
    process (other program) that owns it.
    Problem space:
    If the file is already a certain size when the LV program opens it for reading, it needs to read in that
    in it's entirety and update the chart with the complete data set as quick as possible. Then, it needs
    to continue to check the file about every 250ms for new data and update the chart if there is new data.
    So....How can I initially read in the file and update the chart as fast as possible, then use a controlled loop
    rate of 250ms to continue to read in that same file if there is new data?
    Suggestions...anyone? An algorithm? A rough diagram? alms for the blind?   ;~)
    Gary.

    try to read the file readonly.
    Otherwise LabVIEW could block the writing or not succeed in opening the file.
    And try reading the filesize and read again when data is available.
    greetings from the Netherlands

  • Write / read objects to/from File

    Hello,
    I have a problem with the writing and reading objects to/from a file.
    So what excatly I'm trying to is:
    I have a class Data. A thread creates constantly (until some limit) objects of this class Data.
    Once an object is created, is placed in a buffer (I wrote my own class DataBuffer extention of LinkedList).
    I have a limit on this buffer, so when the limit is reached, another thread starts getings objects from the buffer, writes them on the disk and removing them from the buffer.
    I'm putting the object always on the "top" and I'm reading them and removing always from the "bottom" of the buffer (FIFO).
    For writing the objects Data to the file, I'm using a FileOutputStream and ObjectOutputStream:
    I have something like that:
    FileOutputStream fos = new FileOutputStrea("file_name");
    ObjectOutputStream oos = new ObjectOutputStream(fos);after that I have this:
    while(size > i ){
                        try {
                             Data d = buff.getData();
                             //write to File
                             oos.writeObject(buff.getData());
                             oos.flush();
                             buff.removeData();
                             System.out.println("written data " + d);
                             i++;
                        } catch (IOException e) {
                             e.printStackTrace();
                   }here size is the limit for the buffer.
    So, when the size is equal to the total number of objects that were created and placed in the buffer, I have no problems with the writing/reading from the file.
    But if size is equal, for example, to the half of the total number of objects that were created, I have a problem.
    A more detailed exmple is:
    10 objects were created, the 10 objects were places to the buffer, a test was made and all the objects were written on the disk -> writing and reading are OK.
    if 10 objects were created, and I want to write 5 objects, and after that more 5, the first thing I remarqued is that the size of the file is different from the size of the file created by the first method. Second thing is when I'm reading from the file I can read only 5 objects and after that I have this exception:
    java.io.StreamCorruptedException : invalid type code : AC
    So ... is anyone has an idea why I have this problem?
    PS. For reading from the file I'm using another thread that executes this code:
    fis = new FileInputStream(readFile);
                        ois = new ObjectInputStream(fis);
                        while(true){
                             try{
                                  Data d = (Data)ois.readObject();
                                  System.out.println("data read " + d); //here I'm displaying the object that was read
                                  buff.putData(d);
                             }catch(EOFException e){
                                  break;
                        ois.close();
                        fis.close();
                   }catch (FileNotFoundException e) {
                        e.printStackTrace();
                   }catch(IOException e){
                        e.printStackTrace();
                   }catch (ClassNotFoundException e) {
                        e.printStackTrace();
                   }Regards,
    Anton
    PS. At present I'm writing first all the Data objects, and after that I'm trying to read from the file.
    Edited by: anton_tonev on Oct 5, 2007 4:14 AM

    Hello again :)
    Finally I found the solution :)
    In fact it is important to not to close the FileOutputStream and ObjectOutputStream until all the data was written on the disk.
    What I mean:
    in the begining of the program you have to open these two streams and write down the data that comes from somewhere.
    But you musn't close these streams before the final writing on the disk.
    It must look like this:
    1 Open file stream & opent object stream
    2 you're writing your all objects: one by one; 10 by 10 or as you wish ... but after each writing down you don't close the streams
    3 you already finished with the writing, (the end of your program), now you can close the streams.
    Unfortunatly I don't know why if I write the data and close the stream every time, after that it is impossible to append new data (with reopen the file)

  • What's the best way for reading this binary file?

    I've written a program that acquires data from a DAQmx card and writes it on a binary file (attached file and picture). The data that I'm acquiring comes from 8 channels, at 2.5MS/s for, at least, 5 seconds. What's the best way of reading this binary file, knowing that:
    -I'll need it also on graphics (only after acquiring)
    -I also need to see these values and use them later in Matlab.
    I've tried the "Array to Spreadsheet String", but LabView goes out of memory (even if I don't use all of the 8 channels, but only 1).
    LabView 8.6
    Solved!
    Go to Solution.
    Attachments:
    AcquireWrite02.vi ‏15 KB
    myvi.jpg ‏55 KB

    But my real problem, at least now, is how can I divide the information to get not only one graphic but eight?
    I can read the file, but I get this (with only two channels):
    So what I tried was, using a for loop, saving 250 elements in different arrays and then writing it to the .txt file. But it doesn't come right... I used 250 because that's what I got from the graphic: at each 250 points it plots the other channel.
    Am I missing something here? How should I treat the information coming from the binary file, if not the way I'm doing?
    (attached are the .vi files I'm using to save in the .txt format)
    (EDITED. I just saw that I was dividing my graph's data in 4 just before plotting it... so It isn't 250 but 1000 elements for each channel... Still, the problem has not been solved)
    Message Edited by Danigno on 11-17-2008 08:47 AM
    Attachments:
    mygraph.jpg ‏280 KB
    Read Binary File and Save as txt - 2 channels - with SetFilePosition.vi ‏14 KB
    Read Binary File and Save as txt - with SetFilePosition_b_save2files_with_array.vi ‏14 KB

  • "Read from Binary File" and efficiency

    For the first time I have tried using Read from Binary File on sizable data files, and I'm seeing some real performance problems. To prevent possible data loss, I write data as I receive them from DAQ, 10 times per second. What I write is a 2-D array of double, 1000 data points x 2-4 channels. When reading in the file, I wish I could read it as a 3-D array, all at once. That doesn't seem supported, so I repeatedly do reads of 2-D array, and use a shift register with Build Array to assemble the 3-D array that I really need. But it's incredibly slow! It seems that I can only read a few hundred of these 2-D items per second.
    It also occurred to me that the Build Array being used in a shift register to keep adding on to the array, could be a quadratic-time operation depending on how it is implemented. Continually and repeatedly allocating bigger and bigger chunks of memory, and copying the growing array at every step.
    So I'm looking for suggestions on how to efficiently store, efficiently read, and efficiently reassemble my data into the 3-D array that I need. Perhaps I could simplify life if I had  "write raw data" and "read raw data" operations that write and read only the numbers and no metadata.then I could write the file and read it back in any size chunks I please -- and read it with other programs besides. But I don't see them in the menus.
    Suggestions?
    Ken
    Solved!
    Go to Solution.

    I quote the detailed help from Read from Binary File:
    data type sets the type of data the function uses to read from
    the binary file. The function interprets the data starting at the current file
    position to be count instances of data type.
    If the type is an array, string, or cluster containing an array or string, the
    function assumes that each instance of that data type contains size information.
    If an instance does not include size information, the function misinterprets the
    data. If LabVIEW determines that the data does not match the type, it sets data
    to the default for the specified type and returns an error.
    So I see how I could write data without any array metadata by turning off "prepend array or string size information", but I don't see any way to read it back in such bare form. If I did, I'd have to tell it how long an array to read, and I don't see where to do that. If I could overcome this, I could indeed read in much larger chunks.
    I'll try the auto-indexing tunnel anyway. I didn't tell you the whole truth, the 3-D array is actually further sliced up based on metadata that I carry, and ends up as a 4-D array of "runs". But I can do that after the fact instead of trying to do it with shift registers as I'm reading.
    Thanks,
    Ken

  • Binary file reading confusion

    Hi there,
    I'm trying to read a partially binary file with the following structure: The first three lines of the file are regular strings (one can see them correctly when opening the file in a text editor), the rest of the file's contents are integers in their binary form (one cannot see them correctly when opening the file in a text editor).
    How can I read such a file?
    Thanks in advance!
    +P.S.: If you're interested, I'm trying to read a .PPM file (look  here to find out about the format).+

    pfrc wrote:
    Hi there,
    I'm trying to read a partially binary file with the following structure: The first three lines of the file are regular strings (one can see them correctly when opening the file in a text editor), the rest of the file's contents are integers in their binary form (one cannot see them correctly when opening the file in a text editor).
    How can I read such a file?
    Thanks in advance!
    +P.S.: If you're interested, I'm trying to read a .PPM file (look  here to find out about the format).+Use a BufferedInputStream wrapped around a FileInputStream. Read and bytes and until you've hit two end-of-lines, then put those bytes into a String constructor. Note that you'll have to know which line end convention is used, or write your code so as to allow for any of the common ones. After that, continue reading bytes and processing however is appropriate for you.

  • Read Binary File, T=1

    Hello,
    I'm trying to read a binary file from a card.
    This is something I can easily do, at least until now :)
    I'm testing a new card (with protocol T=1 instead of the T=0 I used so far) and I don't have the card technical document (with the APDUs format) but I guess the APDUs are the same (for the read binary command and other more general commands like the select and others alike).
    I can select the pretended files and navigate in the card EFs/DFs, but for some reason I cant read a complete binary file (it stops before the last block reading).
    This is the code I have used so far without problems:
    private static byte[] readBinary() throws CardException, IOException {
      //CardChannel channel = getCardChannel();
      ByteArrayOutputStream os = new ByteArrayOutputStream(256);
      boolean finished = false;
      byte offset = (byte) 0x00;
      int i=1;
      while (!finished) {
         //Read binary command
         ResponseAPDU resp = channel.transmit(new CommandAPDU(0x00, 0xB0, offset,
                   0x00, READ_BUFFER_LENGTH));
         i++;
         byte[] data = resp.getData();
         offset = (byte) (offset + (byte) 0x01);
         os.write(data);
         if (data.length < READ_BUFFER_LENGTH) {
              finished = true;
      return os.toByteArray();
    }Where READ_BUFFER_LENGTH = 256
    If with the other cards I tested this code works fine why doesn't it work with this card? The only difference is at the transport protocol, this card protocol is T=1 and the other cards was T=0, but I thought that PC/SC would hide the transport protocol layer
    Thanks for any help
    Brian
    Edited by: Brian.Rep on Dec 26, 2008 3:16 AM

    Brian.Rep wrote:
    Hello,
    I'm trying to read a binary file from a card.If it is a Java Card, it would help to see source code of the applet.
    This is something I can easily do, at least until now :)
    I'm testing a new card (with protocol T=1 instead of the T=0 I used so far) and I don't have the card technical document (with the APDUs format) but I guess the APDUs are the same (for the read binary command and other more general commands like the select and others alike).
    I can select the pretended files and navigate in the card EFs/DFs, but for some reason I cant read a complete binary file (it stops before the last block reading).
    This is the code I have used so far without problems:
    Where READ_BUFFER_LENGTH = 256
    If with the other cards I tested this code works fine why doesn't it work with this card? The only difference is at the transport protocol, this card protocol is T=1 and the other cards was T=0, but I thought that PC/SC would hide the transport protocol layerThis is not quite true. Java Card tries to hide the underlying protocol used, but it is not quite possible with T=0. You need to know what APDU case you're expecting for CLA/INS in order to pick up the data field or send outgoing data.
    What error do you get?

Maybe you are looking for

  • More than one editor (computer) for iWeb and using paypal

    I am not a full-time Mac user, but have been asked to complete a website for a client who has started one in iWeb. She wishes to continue her site in iWeb so that she can make future changes to it. Currently, we have different versions of iWeb on our

  • Installing Printers from Windows 2008 R2 printer server using the Cname

    Am having issues installing printers on workstations from the windows 2008 R2 print server using the DNS names. the servername and Full qualified Name works fine. l have tweaked the Registry and Disabled the StrictNameChecking but still getting an er

  • When I text someone that had an iphone, is it suppose to pop up green?

    My boyfriend has the iPhone 4 and after all day of "waiting for activation" I sent him a message and it popped up green and not blue? What's up with that??

  • IMVU Won't Run on my Mac

    I recently (yesterday) got a second-hand Mac desktop from my mother. I am pretty lost, considering I’ve been a PC user for years; I haven’t used a Mac since middle school or earlier. I’m also not that tech-savvy to begin with, so please forgive my co

  • System info for editing?

    Tired of Window PCs, looking to buy an Apple System. I would like to do some video editing but want to make sure I get a powerful enough system from the start. How does the iMac handle HD video editing or should I look a better, faster computer?