Reading file using fscanf in C - file-io

I have a fixed format text file to be read in my C program.
The format of the text file is as below:
w 400341a0
r dfcfa8
w 7b034dd4
The first character is always w or r, while the second element is a 32bit hex digit.
I have written the code as below to read this file line by line :
int main (int argc, char *argv[])
{
FILE* file_ptr;
char rw;
uint32_t address;
int file_read;
uint32_t file_line_ctr = 1;
#if(MODE == DEBUG)
{
printf("DEBUG_MODE\n ");
}
#else
{
/*Do Nothing*/
}
#endif
file_ptr = fopen(argv[10], "r");
if (file_ptr == NULL)
{
printf("Can't Open File\n");
}
else
{
printf("File Opened Successfully\n");
}
file_read = fscanf(file_ptr, "%c %x" , &rw, &address);
printf("%c %x \n", rw, address);
while (file_read!= EOF)
{
file_read = fscanf(file_ptr, "%c %x" , &rw, &address);
file_line_ctr ++;
printf("%c %x \n", rw, address);
}
fclose(file_ptr);
printf("File Length = %d \n", file_line_ctr);
return 1;
}
I get the output in the below format for a file with 100000 entries:
w 400341a0
400341a0
r dfcfa8
dfcfa8
w 7b034dd4
7b034dd4
File Length = 200000
Evidently, I am reading the entries twice for some reason, as well as printing them twice.
Can someone figure out why is it so ?

Well. Here is a fix
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
FILE* file_ptr;
char rw;
unsigned int address;
int file_read=0;
unsigned int file_line_ctr = 1;
#if(MODE == DEBUG)
{
printf("DEBUG_MODE\n ");
}
#else
{
/*Do Nothing*/
}
#endif
file_ptr = fopen(argv[1], "r");
if (file_ptr == NULL)
{
printf("Can't Open File\n");
}
else
{
printf("File Opened Successfully\n");
}
/*
file_read = fscanf(file_ptr, "%c %x " , &rw, &address);
printf("%c %x \n", rw, address);
*/
while (true)
{
file_read = fscanf(file_ptr, "%c %x " , &rw, &address);
if (file_read == EOF)
break;
file_line_ctr ++;
printf("%c %x \n", rw, address);
}
fclose(file_ptr);
printf("File Length = %d \n", file_line_ctr);
return 1;
}
You will notice that I have added a space after the %x to eat the newlines as white space.
I have also modified your while loop a little to stop the last line being repeat printed. Crude. But it will work until you can rethink the logic. The while (true) may not work with all compilers. I used while (1). Which is non standard but worked for me.

Related

Access .csv file from a dll

I am trying to access a file inside my DLL, but fopen always returns NULL. I am not very familiar with DLL's and I do not know how to debug this problem. I tried to run my program with executable and it runs without an error. I am posting my .c and .h files below, any help appreciated.
ReadFile1.c:
#include "ReadFile1.h"
#define MAX_STR_LEN 257
#define MAX_Color_Maps 17
int __declspec(dllexport) __stdcall ReadF(char* ColorMap)
{
FILE* bookFile;
/* allocation of the buffer for every line in the File */
char* buf = malloc(MAX_STR_LEN);
char* tmp;
/* if the space could not be allocated, return an error */
if (buf == NULL)
{
printf("No memory\n");
return 1;
}
if ((bookFile = fopen("Colormaps2.csv", "r")) == NULL) //Reading a file
{
printf("File could not be opened.\n");
printf("%s",strerror(errno));
return 1;
}
struct Color_decider Colors[MAX_Color_Maps];
int i = 1;
fgets(buf, 256, bookFile);
tmp = strtok(buf, ";");
char* temp1;
temp1= strdup(tmp);
strncpy(Colors[0].Color_maps, temp1, strlen(temp1));
while (i<17)
{
tmp = strtok(NULL, ";");
temp1 = strdup(tmp);
strncpy(Colors[i].Color_maps, temp1, strlen(temp1));
i++;
}
for (int k = 1; k < 17; k++)
{
if (!strcmp(Colors[k].Color_maps, ColorMap)) // Find the input column.
{
for (int j = 0; j < 256; j++)
{
fgets(buf, 256, bookFile);
tmp = strtok(buf, ";");
for (int l = 1; l < k * 3; l++)
tmp = strtok(NULL, ";");
tmp = strtok(NULL, ";");
temp1 = strdup(tmp);
Colors->C[j][0] = atoi(temp1);
tmp = strtok(NULL, ";");
temp1 = strdup(tmp);
Colors->C[j][1] = atoi(temp1);
tmp = strtok(NULL, ";");
temp1 = strdup(tmp);
Colors->C[j][2] = atoi(temp1);
printf("%d\t", Colors->C[j][0]);
printf("%d\t", Colors->C[j][1]);
printf("%d\n", Colors->C[j][2]);
}
break;
}
}
free(buf);
fclose(bookFile);
return 0;
}
ReadFile1.h
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
struct Color_decider {
char Color_maps[10];
int C[256][3];
};
char *strdup(char *org)
{
int org_size;
static char *dup;
char *dup_offset;
/* Allocate memory for duplicate */
org_size = strlen(org);
dup = (char *)malloc(sizeof(char)*org_size+1);
if( dup == NULL)
return( (char *)NULL);
/* Copy string */
dup_offset = dup;
while(*org)
{
*dup_offset = *org;
dup_offset++;
org++;
}
*dup_offset = '\0';
return(dup);
}
int __declspec(dllexport) __stdcall ReadF(char* ColorMap);

argc = 1 always regardless of how many characters or words in a given sentence

I want to print argc to verify the words are being calculated correctly before moving to next section in my code. My code is:
int main(int argc, char *argv[])
{
string s = get_string("Text: ");
//Read letters;
n = strlen(s);
printf("%d\n", n);
printf("%d\n", argc);
Every time I run the program, argc = 1 always, even though the sentence typed has 4-5 words. I'm not sure why the program is not calculating argc correctly. Any help is greatly appreciated.
So you asked to read text from input and calculate letter & word count:
#include <stdio.h>
#include <ctype.h>
int main()
{
char a[1000] = {0};
//read a line from input
scanf("%[^\n]s", a);
int word_count = 0;
int letter_count = 0;
int idx = 0;
// go through the line
while (a[idx]){
//skip spaces
while(a[idx] && isspace(a[idx]))
idx++;
// if next char is a letter => we found a word
if (a[idx])
word_count++;
//skip the word, increment number of letters
while (a[idx] && !isspace(a[idx])){
letter_count++;
idx++;
}
}
printf("word count = %d letter count = %d", word_count, letter_count);
return 0;
}
EDIT : display line count also
#include <stdio.h>
#include <ctype.h>
int main()
{
char a[1000] = {0};
//read everyting from input until character '0' is found
scanf("%[^0]s", a);
int word_count = 0;
int letter_count = 0;
int sent_count = 0;
int idx = 0;
// go through the lines
while (a[idx]){
//skip spaces
//newline is also a space, check and increment counter if found
while(a[idx] && isspace(a[idx])){
if (a[idx] == '\n')
sent_count++;
idx++;
}
// if next char is a letter => we found a word
if (a[idx])
word_count++;
//skip the word, increment number of letters
while (a[idx] && !isspace(a[idx])){
letter_count++;
idx++;
}
}
printf("word count = %d letter count = %d line count = %d", word_count, letter_count, sent_count);
return 0;
}
Here's another way:
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000] = {0};
int word_count = 0;
int letter_count = 0;
while (1){
scanf("%s", a);
// break when word starts with '0'
if (a[0] == '0')
break;
word_count++;
letter_count += strlen(a);
}
printf("word count = %d letter count = %d", word_count, letter_count);
return 0;
}
This way reads input until word starting with character '0' is found

*** Error in `./text_buffer': corrupted double-linked list: 0x089cbd98 **

I'm trying to make the app using FreeTDS. Following are my code
/*
* Purpose: Test to see if row buffering and blobs works correctly.
* Functions: dbbind dbnextrow dbopen dbresults dbsqlexec dbgetrow
*/
#include "common.h"
#include "stdio.h"
#include<stdlib.h>
#include "readline/readline.h"
#include "readline/history.h"
#include "string.h"
int
main(int argc, char **argv)
{
char failed;
int hasil;
char ch;
char* r = malloc(30);
LOGINREC *login;
DBPROCESS *dbproc;
int i;
int p = 0;
char teststr[1024];
DBINT testint;
read_login_info(argc, argv);
fprintf(stdout, "Starting %s\n", argv[0]);
/* Fortify_EnterScope(); */
while(1)
{
char qrcode[50];
char perintahsql[100] = "select * from REF_COBA where id=";
char *alokasi="";
while(ch != '\n') // terminates if user hit enter
{
ch = getchar();
qrcode[p] = ch;
p++;
}
qrcode[p]='\0';
strcpy(r,qrcode);
alokasi = strsep(&r, ",");
strcpy(r,"");
p=0;
ch = "";
dbinit();
dberrhandle(syb_err_handler);
dbmsghandle(syb_msg_handler);
fprintf(stdout, "About to logon\n");
login = dblogin();
DBSETLPWD(login, PASSWORD);
DBSETLUSER(login, USER);
DBSETLAPP(login, "text_buffer");
//DBSETLHOST(login, "ntbox.dntis.ro");
DBSETLHOST(login, "192.168.1.9");
fprintf(stdout, "About to open\n");
dbproc = dbopen(login, SERVER);
if (strlen(DATABASE))
dbuse(dbproc, DATABASE);
dbloginfree(login);
#ifdef MICROSOFT_DBLIB
dbsetopt(dbproc, DBBUFFER, "100");
#else
dbsetopt(dbproc, DBBUFFER, "100", 0);
#endif
dbcmd(dbproc, strcat(perintahsql,alokasi));
dbsqlexec(dbproc);
if (dbresults(dbproc) != SUCCEED)
{
failed = 1;
fprintf(stdout, "Was expecting a result set.");
exit(1);
}
fprintf(stdout, "select\n");
for (i = 1; i <= dbnumcols(dbproc); i++)
printf("col %d is %s\n", i, dbcolname(dbproc, i));
fprintf(stdout, "setelah for\n");
dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
dbbind(dbproc, 2, CHARBIND, 0, (BYTE *) teststr);
for (i = 1; i < 2; i++)
{
char expected[1024];
sprintf(expected, "row %03d", i);
if (i % 100 == 0)
{
dbclrbuf(dbproc, 100);
}
if (REG_ROW != dbnextrow(dbproc))
{
failed = 1;
fprintf(stderr, "Failed. Expected a row\n");
//exit(1);
}
else
{
fprintf(stdout, "jalanin usb\n");
printf("Read a row of data -> %d %s\n", (int) testint, teststr);
hasil = system("/opt/usb1rc_linux USB1REL-172 R");
sleep(2);
hasil = system("/opt/usb1rc_linux USB1REL-172 r");
}
}
dbfreebuf(dbproc);
dbexit();
}
return 0;
}
I modified the source from FreeTDS, after compilation there are some warning
bash-4.2# make text_buffer
CC text_buffer.o
text_buffer.c: In function 'main':
text_buffer.c:54:8: warning: assignment makes integer from pointer without a cast [enabled by default]
ch = "";
^
text_buffer.c:18:7: warning: variable 'hasil' set but not used [-Wunused-but-set-variable]
int hasil;
^
text_buffer.c:17:8: warning: variable 'failed' set but not used [-Wunused-but-set-variable]
char failed;
^
CC common.o
CCLD text_buffer
I execute the program without error. This program will check input from user then run the sql command. The format of input shall be , e.g 123456,john doe then press enter. After 50-60 times of input, the double-linked list error will happen.
could anybody help why the error happen?

How to change this app to disable input from command line?

This is the Original code:
#include <stdio.h>
#include <string.h>
#include <assert.h>
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#else
#include <sys/select.h>
#endif
#include <sphinxbase/err.h>
#include <sphinxbase/ad.h>
#include "pocketsphinx.h"
static const arg_t cont_args_def[] = {
POCKETSPHINX_OPTIONS,
/* Argument file. */
{"-argfile",
ARG_STRING,
NULL,
"Argument file giving extra arguments."},
{"-adcdev",
ARG_STRING,
NULL,
"Name of audio device to use for input."},
{"-infile",
ARG_STRING,
NULL,
"Audio file to transcribe."},
{"-inmic",
ARG_BOOLEAN,
"no",
"Transcribe audio from microphone."},
{"-time",
ARG_BOOLEAN,
"no",
"Print word times in file transcription."},
CMDLN_EMPTY_OPTION
};
static ps_decoder_t *ps;
static cmd_ln_t *config;
static FILE *rawfd;
static void
print_word_times()
{
int frame_rate = cmd_ln_int32_r(config, "-frate");
ps_seg_t *iter = ps_seg_iter(ps);
while (iter != NULL) {
int32 sf, ef, pprob;
float conf;
ps_seg_frames(iter, &sf, &ef);
pprob = ps_seg_prob(iter, NULL, NULL, NULL);
conf = logmath_exp(ps_get_logmath(ps), pprob);
printf("%s %.3f %.3f %f\n", ps_seg_word(iter), ((float)sf / frame_rate),
((float) ef / frame_rate), conf);
iter = ps_seg_next(iter);
}
}
static int
check_wav_header(char *header, int expected_sr)
{
int sr;
if (header[34] != 0x10) {
E_ERROR("Input audio file has [%d] bits per sample instead of 16\n", header[34]);
return 0;
}
if (header[20] != 0x1) {
E_ERROR("Input audio file has compression [%d] and not required PCM\n", header[20]);
return 0;
}
if (header[22] != 0x1) {
E_ERROR("Input audio file has [%d] channels, expected single channel mono\n", header[22]);
return 0;
}
sr = ((header[24] & 0xFF) | ((header[25] & 0xFF) << 8) | ((header[26] & 0xFF) << 16) | ((header[27] & 0xFF) << 24));
if (sr != expected_sr) {
E_ERROR("Input audio file has sample rate [%d], but decoder expects [%d]\n", sr, expected_sr);
return 0;
}
return 1;
}
/*
* Continuous recognition from a file
*/
static void
recognize_from_file()
{
int16 adbuf[2048];
const char *fname;
const char *hyp;
int32 k;
uint8 utt_started, in_speech;
int32 print_times = cmd_ln_boolean_r(config, "-time");
fname = cmd_ln_str_r(config, "-infile");
if ((rawfd = fopen(fname, "rb")) == NULL) {
E_FATAL_SYSTEM("Failed to open file '%s' for reading",
fname);
}
if (strlen(fname) > 4 && strcmp(fname + strlen(fname) - 4, ".wav") == 0) {
char waveheader[44];
fread(waveheader, 1, 44, rawfd);
if (!check_wav_header(waveheader, (int)cmd_ln_float32_r(config, "-samprate")))
E_FATAL("Failed to process file '%s' due to format mismatch.\n", fname);
}
if (strlen(fname) > 4 && strcmp(fname + strlen(fname) - 4, ".mp3") == 0) {
E_FATAL("Can not decode mp3 files, convert input file to WAV 16kHz 16-bit mono before decoding.\n");
}
ps_start_utt(ps);
utt_started = FALSE;
while ((k = fread(adbuf, sizeof(int16), 2048, rawfd)) > 0) {
ps_process_raw(ps, adbuf, k, FALSE, FALSE);
in_speech = ps_get_in_speech(ps);
if (in_speech && !utt_started) {
utt_started = TRUE;
}
if (!in_speech && utt_started) {
ps_end_utt(ps);
hyp = ps_get_hyp(ps, NULL);
if (hyp != NULL)
printf("%s\n", hyp);
if (print_times)
print_word_times();
fflush(stdout);
ps_start_utt(ps);
utt_started = FALSE;
}
}
ps_end_utt(ps);
if (utt_started) {
hyp = ps_get_hyp(ps, NULL);
if (hyp != NULL) {
printf("%s\n", hyp);
if (print_times) {
print_word_times();
}
}
}
fclose(rawfd);
}
/* Sleep for specified msec */
static void
sleep_msec(int32 ms)
{
#if (defined(_WIN32) && !defined(GNUWINCE)) || defined(_WIN32_WCE)
Sleep(ms);
#else
/* ------------------- Unix ------------------ */
struct timeval tmo;
tmo.tv_sec = 0;
tmo.tv_usec = ms * 1000;
select(0, NULL, NULL, NULL, &tmo);
#endif
}
/*
* Main utterance processing loop:
* for (;;) {
* start utterance and wait for speech to process
* decoding till end-of-utterance silence will be detected
* print utterance result;
* }
*/
static void
recognize_from_microphone()
{
ad_rec_t *ad;
int16 adbuf[2048];
uint8 utt_started, in_speech;
int32 k;
char const *hyp;
if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),
(int) cmd_ln_float32_r(config,
"-samprate"))) == NULL)
E_FATAL("Failed to open audio device\n");
if (ad_start_rec(ad) < 0)
E_FATAL("Failed to start recording\n");
if (ps_start_utt(ps) < 0)
E_FATAL("Failed to start utterance\n");
utt_started = FALSE;
E_INFO("Ready....\n");
for (;;) {
if ((k = ad_read(ad, adbuf, 2048)) < 0)
E_FATAL("Failed to read audio\n");
ps_process_raw(ps, adbuf, k, FALSE, FALSE);
in_speech = ps_get_in_speech(ps);
if (in_speech && !utt_started) {
utt_started = TRUE;
E_INFO("Listening...\n");
}
if (!in_speech && utt_started) {
/* speech -> silence transition, time to start new utterance */
ps_end_utt(ps);
hyp = ps_get_hyp(ps, NULL );
if (hyp != NULL) {
printf("%s\n", hyp);
fflush(stdout);
}
if (ps_start_utt(ps) < 0)
E_FATAL("Failed to start utterance\n");
utt_started = FALSE;
E_INFO("Ready....\n");
}
sleep_msec(100);
}
ad_close(ad);
}
int
main(int argc, char *argv[])
{
char const *cfg;
config = cmd_ln_parse_r(NULL, cont_args_def, argc, argv, TRUE);
/* Handle argument file as -argfile. */
if (config && (cfg = cmd_ln_str_r(config, "-argfile")) != NULL) {
config = cmd_ln_parse_file_r(config, cont_args_def, cfg, FALSE);
}
if (config == NULL || (cmd_ln_str_r(config, "-infile") == NULL && cmd_ln_boolean_r(config, "-inmic") == FALSE)) {
E_INFO("Specify '-infile <file.wav>' to recognize from file or '-inmic yes' to recognize from microphone.\n");
cmd_ln_free_r(config);
return 1;
}
ps_default_search_args(config);
ps = ps_init(config);
if (ps == NULL) {
cmd_ln_free_r(config);
return 1;
}
E_INFO("%s COMPILED ON: %s, AT: %s\n\n", argv[0], __DATE__, __TIME__);
if (cmd_ln_str_r(config, "-infile") != NULL) {
recognize_from_file();
} else if (cmd_ln_boolean_r(config, "-inmic")) {
recognize_from_microphone();
}
ps_free(ps);
cmd_ln_free_r(config);
return 0;
}
#if defined(_WIN32_WCE)
#pragma comment(linker,"/entry:mainWCRTStartup")
#include <windows.h>
//Windows Mobile has the Unicode main only
int
wmain(int32 argc, wchar_t * wargv[])
{
char **argv;
size_t wlen;
size_t len;
int i;
argv = malloc(argc * sizeof(char *));
for (i = 0; i < argc; i++) {
wlen = lstrlenW(wargv[i]);
len = wcstombs(NULL, wargv[i], wlen);
argv[i] = malloc(len + 1);
wcstombs(argv[i], wargv[i], wlen);
}
//assuming ASCII parameters
return main(argc, argv);
}
#endif
I can compile it by this command:
g++ -o output continuous.cpp -DMODELDIR=\"`pkg-config --variable=modeldir pocketsphinx`\" `pkg-config --cflags --libs pocketsphinx sphinxbase`
And run it by this command : output -inmic yes .
But I like to convert the code as it has no need to get inmic yes and it automatically starts the program from microphone. But I got segmentation fault(core dumped) error when I changed these parts:
static const arg_t cont_args_def= {"-inmic",
ARG_BOOLEAN,
"no",
"Transcribe audio from microphone."};
int main(int argc, char *argv[])
{
config = cmd_ln_parse_r(NULL, cont_args_def, argc, argv, TRUE);
if (cmd_ln_boolean_r(config, "-inmic")) {
recognize_from_microphone();
}
// recognize_from_microphone();
ps_free(ps);
cmd_ln_free_r(config);
return 0;
}
I searched a lot and red the documentation but couldn't understand what's the problem?
Change the last argument passed to cmd_ln_parse_r from TRUE to FALSE.
It has something to do with strict checking.
I figured this out by reading the source code for cmd_ln.c in the sphinxbase code.
I also changed the boolean value for -inmic in cont_args_def from "no" to "yes".

Convert decimal to binary and return array

probably there is a smart way to do that , but anyway i get error on this :
-(int*)decimalBinary:(int)decimal
{
int i=0;
int *bin;
while (decimal!=0)
{
bin[i]=decimal%2;
decimal=decimal/2;
i++;
}
return bin;
}
on the modulo line . why ?
And whats the better way to get it to array ?
Declaring
int *bin;
sets aside space for a pointer but doesn't make it point to an object. It is crucial to initialize bin before using it.
To solve your problem you can declare an array bin[4] in caller function (int main) and then pass *bin to your calling function.
The following code is adapted from This answer on how to print an integer in binary format. Storing "binary digits" into an int array is added into the code below:
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
const char *byte_to_binary(long x);
int main(void)
{
long lVal;
int i, len, array[18];
char buf[18];
{ /* binary string to int */
char *tmp;
char *b = "11010111001010110";
lVal=strtol(b, &tmp, 2); //convert string in "base 2" format to long int
printf("%d\n", lVal);
}
{
printf("%s", byte_to_binary(lVal));
/* byte to binary string */
sprintf(buf,"%s", byte_to_binary(lVal));
}
len = strlen(buf);
for(i=0;i<len;i++)
{ //store binary digits into an array.
array[i] = (buf[i]-'0');
}
getchar();
return 0;
}
const char *byte_to_binary(long x)
{
static char b[17]; //16 bits plus '\0'
b[0] = '\0';
char *p = b;
int z;
for (z = 65536; z > 0; z >>= 1) //2^16
{
*p++ = (x & z) ? '1' : '0';
}
return b;
}