Store data dynamically after reading from a file - c

I am trying to read strings from a file separated by commas, and want to display and save the result into a particular format (which looks like a JSON format) during output.
I managed to read and display data from a file but failed to dynamically display it in a format which looks like the example below. Instead, it just displays the entire string in one line before ending.
e.g.
Contents of the file:
Distance, 50km, Time, 2 hrs, Date, 1 Jan 2015, etc.
Desired output result:
{"Distance":"50km"}
{"Time":"2hrs"}
{"Date":"1 Jan 2015"}
Actual Output
{" Entire contents from the file " : " nothing appears here "}
I've commented out the lines that deals with reading the file contents until it finds a comma, and where it prints the result in the desired format as those lines didn't work correctly.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( int argc, char *argv[] )
{
if ( argc != 2 )
{ printf( "Insert filename you wish to open\n Eg: %s filename\n\n", argv[0] );
}
else
{
FILE *file = fopen( argv[1], "r" );
if ( file == 0 )
{
printf( "There was an error opening the file\n\n" );
}
else
{
char a,b;
while ( ( a = fgetc( file ) ) != EOF )
{
printf( "%c", a,b );
//fscanf(file,"%[^,]",a);/* Read data until a comma is detected, */
// printf("\nData from file:\n{\"%s\" : \"%s\"}\n\n",a,b); /* Display results into {"A":"B"} format */
}
fclose( file );
}
}
return 0;
}

Use this code:
char a[50], b[50];
while(1 == fscanf(file," %[^,]",a) ) /* Read data until a comma is detected, */
{
fgetc( file ); // Ignore , character
fscanf(file," %[^,]",b); /* Read data until a comma is detected, */
fgetc( file ); // Ignore , character
printf("{\"%s\":\"%s\"}\n",a,b); /* Display results into {"A":"B"} format */
}
Live demo here
Note the extra space before format in fscanf
The algorithm to read is:
while( is reading [a] succesful )
{
ignore comma
read [b]
ignore comma
print {"a": "b"}
}

Related

Detect if line is the last line of a file, using only one loop?

I am writing a text file parser in C.
I would like to read each line of a text file using fgets, except for the very last line, which I would like to skip.
Also, there is no telling how many characters will be in the file or in the last line, but assume my parser only cares about the first LINEMAXLEN characters in each line.
Currently, the only way I can think to do this is by running two loops, something like the following:
char line[ LINEMAXLEN+1u ];
unsigned int nlines;
unsigned int i;
nlines = 0u;
while ( fgets (line, LINEMAXLEN, file) != NULL )
nlines += 1u;
i = 0u;
while ( fgets (line, LINEMAXLEN, file) != NULL ) {
if ( i >= nlines - 1u )
break;
//...parse the line
i += 1u;
}
But surely, there's got to be a smarter way to do it in only one loop, no?
Instead of using two loops, it would be more efficient to always read two lines in advance and to only process a line once the next line has been sucessfully read. That way, the last line will not be processed.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define LINEMAXLEN 30
//forward function declarations
void process_line( const char *line );
bool read_start_of_line_and_discard_rest( char buffer[], int buffer_size, FILE *fp );
int main( void )
{
FILE *fp;
char lines[2][LINEMAXLEN];
//This index specifies which index in the array "lines"
//represents the newest line. The other index is the
//index of the previous line.
int newest_index = 0;
//attempt to open file
fp = fopen( "input.txt", "r" );
if ( fp == NULL )
{
fprintf( stderr, "Error opening file!\n" );
exit( EXIT_FAILURE );
}
//read first line
if ( !read_start_of_line_and_discard_rest( lines[newest_index], LINEMAXLEN, fp ) )
{
fprintf( stderr, "Error reading first line!\n" );
exit( EXIT_FAILURE );
}
//process one line per loop iteration
for (;;)
{
//swap the index, so that the newest line is now the
//previous line
newest_index = !newest_index;
//read the new line
if ( !read_start_of_line_and_discard_rest( lines[newest_index], LINEMAXLEN, fp ) )
{
//we have reached end-of-file, so we don't process the
//previous line, because that line is the last line
break;
}
//since reading in a new line succeeded, we can be sure that
//the previous line is not the last line, so we can process
//the previous line
//process the previous line
process_line( lines[!newest_index] );
}
//cleanup
fclose( fp );
}
//This function will process a line after it has been read
//from the input file. For now, it will only print it.
void process_line( const char *line )
{
printf( "Processing line: %s\n", line );
}
//This function will read exactly one line of input and remove the
//newline character, if it exists. On success, it will return true.
//If this function is unable to read any further lines due to
//end-of-file, it returns false. If it fails for any other reason, it
//will not return, but will print an error message and call "exit"
//instead.
//If the line is too long to fit in the buffer, it will discard
//the rest of the line and report success.
bool read_start_of_line_and_discard_rest( char buffer[], int buffer_size, FILE *fp )
{
char *p;
//attempt to read one line from the stream
if ( fgets( buffer, buffer_size, fp ) == NULL )
{
if ( ferror( fp ) )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
return false;
}
//determine whether line was too long for input buffer
p = strchr( buffer, '\n' );
if ( p == NULL )
{
int c;
//discard remainder of line
do
{
c = getchar();
} while ( c != EOF && c != '\n' );
}
else
{
//remove newline character by overwriting it with a null
//character
*p = '\0';
}
return true;
}
For the input
This is line1.
This is line2 which has an additional length longer than 30 characters.
This is line3.
This is line4.
this program has the following output:
Processing line: This is line1.
Processing line: This is line2 which has an ad
Processing line: This is line3.
As you can see, all lines except the last line are being processed, and only the first LINEMAXLEN-1 (30-1 in my example) characters of each line are being processed/stored. The remaining characters are being discarded.
Only LINEMAXLEN-1 instead of LINEMAXLEN characters from each line are being processed/stored because one character is required to store the terminating null character.
This is quite simple to do in a single loop if we use alternating buffers [as others have mentioned].
In the loop below we read a line into the "current" buffer. If not the first line, we process the previous line in the "other" buffer.
By alternating the index into a buffer pool of two buffers, we avoid unnecessary copying.
This introduces a delay in the processing of the buffer. On the last iteration, the last line will be in the current buffer, but it will not be processed.
#define LINEMAXLEN 1000 // line length of buffer
#define NBUF 2 // number of buffers
char lines[NBUF][LINEMAXLEN]; // buffer pool
int previdx = -1; // index of bufs for _previous_ line
int curidx = 0; // index of bufs for _current_ line
char *buf; // pointer to line buffer to process
// read all lines into alternating line buffers
for (; fgets(lines[curidx],LINEMAXLEN,stdin) != NULL;
previdx = curidx, curidx = (curidx + 1) % NBUF) {
// process _previous_ line ...
if (previdx >= 0) {
buf = lines[previdx];
// process line ...
}
}
fgets() will not modify the buffer at all when it reaches EOF, so just read lines until fgets() returns NULL. The last line read will be retained:
#include <stdio.h>
int main( int argc, char **argv )
{
char line[ 1024 ];
FILE *f = fopen( argv[ 1 ], "r" );
if ( NULL == f )
{
return( 1 );
}
for ( ;; )
{
char *p = fgets( line, sizeof( line ), f );
if ( NULL == p )
{
break;
}
}
printf( "last line: %s\n", line );
return( 0 );
}
This relies on the required behavior of fgets():
The fgets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned.
Robust code should check for errors with ferror().
Working that into your text processing is left as an exercise... ;-)

How can I read different lines of a text file with fgets?

For example, if a .txt has
Hello
There.
written in it, no matter how bigger N is in fgets(str, N, file), it will only store "Hello" in str, because it stops when it finds a '\n' character.
So, how could I read the whole file if, for example, I wanted to find a specific word in it?
So, how could I read the whole file
In order to read the whole file into a memory buffer, you could use the function fread. After turning the input into a string by appending a terminating null character, you could then use the function strstr to search the input for a certain word.
Here is a program which does this and searches the input for the word targetword:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
FILE *fp;
char buffer[1000];
size_t read;
//open input file
fp = fopen( "input.txt", "rt" );
if ( fp == NULL )
{
fprintf( stderr, "ERROR: Unable to open input file!\n" );
exit( EXIT_FAILURE );
}
//read entire file into buffer
read = fread( buffer, 1, sizeof buffer, fp );
//verify that buffer was not too small
if ( read == sizeof buffer )
{
fprintf( stderr, "ERROR: Memory buffer is too small to contain entire input!\n" );
exit( EXIT_FAILURE );
}
//add terminating null character to make input a valid
//null-terminated string
buffer[read] = '\0';
//search input for target word
if ( strstr( buffer, "targetword" ) != NULL )
printf( "Found word!\n" );
else
printf( "Did not find word!\n" );
fclose( fp );
}
However, instead of reading the entire file at once (which could require a very large memory buffer), it is more common to read one line at a time in a loop, and in every loop iteration, you check whether the current line contains the word you are looking for. That way, the memory buffer only has to be large enough to store one line of input at once, instead of the entire input.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main( void )
{
FILE *fp;
char line[100];
bool found = false;
//open input file
fp = fopen( "input.txt", "rt" );
if ( fp == NULL )
{
fprintf( stderr, "ERROR: Unable to open input file!\n" );
exit( EXIT_FAILURE );
}
//read one line per loop iteration
while ( fgets( line, sizeof line, fp ) != NULL )
{
//verify that line was not too long to fit into buffer
if ( strchr( line, '\n' ) == NULL )
{
fprintf( stderr, "line too long to fit buffer!\n" );
exit( EXIT_FAILURE );
}
//search for target word
if ( strstr( line, "targetword" ) != NULL )
{
found = true;
break;
}
}
if ( found )
printf( "Found word!\n" );
else
printf( "Did not find word!\n" );
fclose( fp );
}
However, both solutions have several possible issues:
If the target word targetword is part of another word, for example thetargetword, then it will state that it found the target word. I'm not sure if this is what you want or if you want the target word to appear by itself.
If the target word is syllabified so that, for example, target-\n appears in one line and word in the next line, then the program won't be able to find the word.
The search is case-sensitive, so it will only find targetword, but not Targetword or TARGETWORD.
All of these issues can be solved, if necessary, but would require additional work.

How do I read the rows of a specific column in c?

I've tried multiple stuff as reading new lines after checking if that columns is "General" but still does not work at all. It is an csv file and it would be lines with commas after each fgets and i need a specific column with it's data.
Here's my code:
char fi[1024];
while(!feof(CsvFile)){
//Read
fgets(fi, 1024, CsvFile);
if(strstr(fi, "General") == 0){
fscanf(CsvFile, "%[^\n]s", fi);
printf("%s", fi);
}
fgetc(CsvFile);
}
It does not print what i want.
Reading a CSV file is much more complicated than you assumed (see https://www.rfc-editor.org/rfc/rfc4180). You must take all kind of rules into account. For instance, if a cell contains a comma, the content must be surrounded by ".
However, you can implement a simplified version which assumes:
a CSV file is made of lines;
a line is MAX_LINE characters, at most;
a line is made of cells;
a cell ends with comma or new-line;
a cell contains anything but comma or new-line.
The code below reads one line at a time and then uses strtok to split the line into cells.
Welcome to SO and good luck!
#include <stdio.h>
#include <string.h>
#define MAX_LINE 1024
int main( int argc, char* argv[] )
{
//
FILE* fp = fopen( "c:\\temp\\so.txt", "r" );
if ( !fp )
{
printf( "could not open file" );
return -1;
}
//
char line[ MAX_LINE + 1 ];
while ( fgets( line, sizeof( line ) / sizeof( *line ), fp ) ) // get a line
{
int col_idx = 0;
const char* sep = "\r\n,"; // cells are separated by a comma or a new line
char* cell = strtok( line, sep ); // find first cell
while ( cell )
{
// your processing code goes here
cell = strtok( NULL, sep ); // next cell
col_idx++;
}
}
return 0;
}

reading certain characters with fgetc

I am trying to create a spell checking program that takes an input file and makes sure each word is correct by searching through a dictionary file. The problem i am facing is that when i try to take each word seperated by spaces from the input file and put it into a char [] the words with " for some reason print
H0
i1
c0
h1
r2
i3
s4
!5
â0
1
2
h3
o4
w5
w6
â7
8
9
a0
r1
42
e3
y0
o1
u2
.3
the integers are my index
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "dict.h"
int main(int argc, char *argv[] ) {
FILE *fdict,*input;
int i;
char ch;
/* the biggest posible word is 30 plus a possible of two " or ' characters and the null character. so the limit of the array is 33*/
char norm[33];
if ( argc < 3 ) /* argc should be 3 for correct execution*/
{
fprintf(stderr,"1 or 2 Files were missing.");
exit(1);
}
if ( argc > 3 ){
fprintf(stderr,"too many Arguments");
exit(1);
}
/* We assume argv[1] and agrv[2] are filenames to open*/
fdict = fopen( argv[1], "r" );/* file pointer for the dictionary file*/
input = fopen( argv[2], "r" );/*file pointer for the input file*/
/* fopen returns NULL on failure */
if ( fdict == NULL ){
fprintf(stderr,"Could not open file: %s\n", argv[1] );/*checks to make sure the dictionary file can be opened*/
exit(1);
}
if ( input == NULL ){
fprintf(stderr,"Could not open file: %s\n", argv[2] );/*checks to make sure the input file can be opened*/
exit(1);
}
/* Read one character at a time from file, stopping at EOF, which
indicates the end of the file. Note that the idiom of "assign
to a variable, check the value" used below works because
the assignment statement evaluates to the value assigned. */
while ( ( ch = fgetc( input ) ) != EOF ) {
char word[33] = "";/* resets the array*/
for ( i = 0; !isspace( ch ) ; i++ ){
word[i] = ch;
printf("%c%d\n",ch,i);/* checking to see what is wrong with the index*/
ch = fgetc( input );
}
}
fclose( fdict );
fclose( input );
return 0;
}
my input looks like:
Hi chris! “howw” are you.
" is not the same as “ nor ”. (3 different quote marks.) Based on different encodings, these 3 characters use various sequences of char to represent them yet code only prints one char at a time.
Suggest just using the simpe quote mark ".
A simple or programmer's text editor would do. Avoid a word processor that may bring in non-ASCII quote marks until your code is ready for that (#n.m.)

Replace string with another

I am just not sure why my replaceWord isn't going in to the file at all i have used all the commented out and so on and so forth. I am just trying to replace with with the text received from the command line argument. I know i might be far off I was just looking for a relatively easy way to do it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( int argc, char *argv[] )
{
if ( argc != 4 ) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename\n", argv[0] );
}
else
{
// We assume argv[1] is a filename to open
char* wordReplace = argv[1];
char* replaceWord = argv[2];
FILE *file = fopen( argv[3], "r" );
/* fopen returns 0, the NULL pointer, on failure */
if ( file == 0 )
{
printf( "Could not open file\n" );
}
else
{
char string[100];
int len = 0;
/* read one character at a time from file, stopping at EOF, which
indicates the end of the file. Note that the idiom of "assign
to a variable, check the value" used below works because
the assignment statement evaluates to the value assigned. */
while ( (fscanf( file, "%s", string ) ) != EOF )
{
len = strlen(string);
printf( "%s\n", string );
if(strcmp(string, wordReplace) == 0){
//fseek (file, (-strlen(string) + 1), 1);
//fputc(*replaceWord,file);
//replaceWord++;
//strcpy(string, replaceWord);
fprintf(file,"%s",replaceWord);
fputs(replaceWord, file);
printf("\n%d\n", len);
}
}
fclose( file );
}
}
printf("\n");
return 0;
}
You've opened the file in r ie read mode and trying to write to it.
Also after correcting that, note that, the replaced word and word to be replaced have to be of the same size, if you want to replace the file in place. Else you will end up overwriting other data. And you need to use functions like fseek to reposition the internal file pointer as fp would have moved ahead after fscanf

Resources