I am actually writing in the C language. I have a text file that has this format (100 times):
time: 0 ms
switch0: 1
switch1: 1
switch2: 1
switch3: 1
potentiometer: 0.50
temperature: 0.14
light: 0.90
---------------------------
time: 40 ms
switch0: 1
switch1: 1
switch2: 1
switch3: 1
potentiometer: 0.65
temperature: 0.20
light: 0.89
---------------------------
and I am trying to parse the numbers so that I can modify them in Matlab. Here's the code I have so far:
#include <stdio.h>
#include <math.h>
int main(void) {
FILE *readtheprices;
readtheprices = fopen("ArduinoTestOutput.txt", "r"); // Open the text file to read
FILE *writetheprices;
writetheprices = fopen("ccodeoutput.csv", "w+"); // Open the csv file to write
int i = 0, its[20], a[20], joke[20];
char c = 0, str[60];
if (NULL == readtheprices || NULL == writetheprices) // Makes sure the files were opened without an issue
{
fprintf(stderr, "Error opening data files\n");
return 11; //Returns 11 if there is an error opening the files.
}
fprintf(writetheprices, "Time,Switch0,Switch1,Switch2,Switch3,Potentiometer,Temperature,Light\n");
do
{
c = fgetc(readtheprices);
if (feof(readtheprices))
{
break;
}
fprintf(writetheprices, "%c", c);
} while (1);
readtheprices = fopen("arduinofinal.txt", "r");
if (fclose(readtheprices) == EOF) { // Closes readtheprices or returns an error
fprintf(stderr, "Error closing infile\n");
return 22;
}
if (fclose(writetheprices) == EOF) { return 33; } //Closes writetheprices or returns an error
return 0;
}
I am having trouble after the do-while loop, I want to do a sscanf to parse the numbers. Is there a better way to do this? Maybe an fscanf would be better? Any hints and help is appreciated.
What language is this, Ansi C? Is this the GCC compiler?
Ansi C is no longer my main language, but I have coded in it, so I might be able to help. Your code didn't format with the indentations so its sort of hard to read. Bare with me :-)
Are you running a Sentinel terminating file loop? It's look like your do-while is missing something, maybe and an else statement.
I didn't see your sscanf call. But I do not think you need it if you do not have any functions.
You can avoid it by declaring an
int c;
variable instead of a char c then perform a demotion into a string char array, followed by a print format with "%c". Although the asymptotic runtime is terrible.
//author:Geo
======
#include <stdio.h>
#define MAXIN 1024 /* max #chars representatives to be inputted */
int main()
{
/* Create a point to the file */
FILE *readThePrices;
FILE *writeThePrices;
/* An integer for to store the character number 0-255 */
int ch;
/* Create an char array to store the character numbers */
char searay[MAXIN];
/* an element control var, that will also be used as */
/* a size variable */
int cnt = 0;
/* an increment var for the outfile */
int j = 0;
/* Open the file and verified it that there is a point */
readThePrices = fopen("Arduino.txt","r");
writeThePrices = fopen("writeThePrices.csv", "w+");
if (readThePrices == NULL || writeThePrices== NULL) {
/* deal with the error */
perror("Error opening file \n");
return -1;
} else {
while (1) {
/* If feof hits, break occurs, which is a zero */
/* Store the integer in asci form */
ch = fgetc(readThePrices);
if ( feof(readThePrices) )
{
break;
}
searay[cnt] = ch;
++cnt;
}
for (;j < cnt;) {
/* Print each character individually */
fprintf(writeThePrices, "%c", searay[j]);
++j;
}
}
fclose(readThePrices);
fclose(writeThePrices);
return 0;
}
=====================
}