read data from file

This is a discussion on read data from file within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that opens a file and reads the data. But since the data information is organized ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    read data from file

    I am writing a program that opens a file and reads the data. But since the data information is organized in strange way, I don't know the best way to put this data into a vector.

    The data is arranged in varying number "cells" and each cell is arranged like the following:

    int64 TimeStamp
    int32 ChanNumber
    int32 SampFreq
    int32 NumValidSamp
    int16 Samp[0]
    .....
    .....
    int16 Samp[511]

    The note on the documentation states:
    "If NumValidSamp < 512, Samp[n], where n>=NumValidSamp, will contain random data."

    I attached how I first attempted this task.

    So, when I collect the samp data alone, I need to be sure to collect only NumValidSamp of data.

    Now that I just wrote this, I should make a object or struct for each cell. But I still need to read that data into the object...

    T H A N K S !
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If the file is ascii, you can just read each element directly into a variable:
    Code:
    infile >> TimeStamp >> ChanNumber >> ...
    If the file is binary, then you would open the file in binary mode, then use read() to read each element:
    Code:
    	infile.open(file_name,ios::binary);
    	infile.read(static_cast<char *> (&TimeStamp), sizeof(TimeStamp));
    	.
    	.
    	.
    	for (int i=0; i<NumValidSamp; i++)
    	{
    	   infile.read(static_cast<char *> (&Samp[i]), sizeof(Samp[i]));
    	}

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    read char *TimeStamp to make an __int64

    I am able to read everything into TimeStamp as a 0-255 value but I am unsure how to get the __int64 TimeStamp value from the char pointer.

    Code:
    unsigned char *get_data(CString file_name, int* file_sz)
    {	
    	ofstream OutData("data.txt");	
    
    	fstream infile;
    	infile.open(file_name, ios_base::in|ios::binary);
    	if(!infile) {
    		cout << "\nCANNOT OPEN FILE";
    		exit(9);
    	}
    
    	long mark = infile.tellg();		//	Mark current position
    	infile.seekg(FILEHEADER);		//	Move past the file header
    	
    	unsigned char TimeStamp;
    	while(!infile.eof()){
    		infile.read(static_cast<unsigned char*>(&TimeStamp), sizeof(TimeStamp));	//	Extracts bytes from the stream until the limit is reached 
    		OutData << "\n" << static_cast<long double>(TimeStamp);	
    	}
    
    	unsigned char *data = new(nothrow) unsigned char;
    
    	return(data);
    }
    Once I learn how to making things into __int64's, I can modify the rest of the code to read the infile as records and store the data so it matches the data type for each element in the "array of" records.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > unsigned char TimeStamp;
    Well if you just declare it as an __int64 here, then you should be fine:
    __int64 TimeStamp;

    No need to change the read at all:
    infile.read(static_cast<unsigned char*>(&TimeStamp), sizeof(TimeStamp)); // Extracts bytes from the stream until the limit is reached

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  2. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 05:28 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21