Show read_file.c syntax highlighted
/* ex: set tabstop=2 expandtab shiftwidth=2 softtabstop=2: */
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
// FILE FORMAT
// 1164631191, 26, 3.439308, 27, 3.749290
// 1164631491, 26, 3.439308, 27, 3.749290
// 1164631791, 26, 3.439308, 27, 3.749290
/*
*
* Author: Nithya Ramanthan
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double Rinterval = -1;
int Rdigits = -1;
extern
void receive_data(int a, int b, double c);
extern
void set_interval(double intrval, int digs);
// Read the file. Ignore text after "#"
static
void read_file(char* file)
{
/* variable to read in the words from the file */
char line[500];
int ctr, last_ctr, comma_ctr, found_time;
int time, node_id;
double temp;
FILE *input_file_pointer;
/* open input file n.b. no test for the case that filled does
* not exist */
if (!(input_file_pointer = fopen(file, "r"))) {
printf("Unable to open file: %s\n", file);
return;
}
memset(line, 0, sizeof(line));
// FORMAT: time (int), node (int), temp (double), node, temp...
while( fgets(line, sizeof(line), input_file_pointer)!=NULL) {
ctr = 0; last_ctr = 0; comma_ctr = 0; found_time = 0;
printf("Line: %s", line);
// Ignore all characters after the pound
while ((line[ctr] != '\n') && (line[ctr] != '#')) {
if (line[ctr] == ',') {
comma_ctr++;
if (!found_time) {
found_time = 1;
comma_ctr = 0;
sscanf(line, "%d", &time);
last_ctr = ctr+1;
}
else if (comma_ctr == 2) {
sscanf(&line[last_ctr], "%d,%lf", &node_id, &temp);
printf(" Got %f\n", temp);
receive_data(time, node_id, temp);
comma_ctr = 0; last_ctr = ctr+1;
}
}
ctr++;
}
// Read any remaining end of line - if it was a valid line
if ( found_time && (comma_ctr > 0)) {
sscanf(&line[last_ctr], "%d,%f", &node_id, &temp);
receive_data(time, node_id, temp);
}
memset(line, 0, sizeof(line));
}
}
int main(int argc, char *argv[])
{
char* file = NULL;
/* Init */
// misc_init(&argc, argv, CVSTAG);
if (argc != 4) {
printf("argc: %d != 4\n",argc);
printf("USAGE: %s <data-file> <interval> <digits>\n",
argv[0]);
exit(1);
}
file = argv[1];
Rinterval = atof(argv[2]);
Rdigits = atoi(argv[3]);
set_interval(Rinterval, Rdigits);
// if ((file = misc_parse_out_option(&argc, argv, "config-file", 'c'))) {
read_file(file);
return 0;
}
See more files for this project here