sscanf and printf overwriting values - c

I am writing a simple c program that reads in a bunch of values from stdin using getline and sscanf.
The program apparently works: I invoke the function reading the line and print the results with printf. The problem arises when I just tried to print the values again, and the output is gibberish!
-------------------------- EDIT -----------------------------
I found the problem, reported in my own answer below. Thanks everyone for pointing me in the right direction!
My test input file is (with the 2 first lines to be skipped):
//---------------------------------------------------------------------------------
// MODEL VALUE
500 // X DIR
600 // Y DIR
10.0 // Grid Spacing
2e-4 // Time step
80001 // Max run iterations
And my code is:
Functions to read different kinds (separate from main just for clearness in the post)
int readint(void)
{
char *line = NULL;
size_t size;
int my_int;
getline(&line, &size, stdin);
sscanf(line,"%d",&my_int);
return my_int;
}
double readfloat(void)
{
char *line = NULL;
size_t size;
float my_float;
getline(&line, &size, stdin);
sscanf(line,"%f",&my_float);
return my_float;
}
char* readstr(void)
{
char *line = NULL;
size_t size;
getline(&line, &size, stdin);
char * token = strtok(line, " ");
return token;
}
int readvoid(void)
{
char *line = NULL;
size_t size;
getline(&line, &size, stdin);
return 0;
}
Main
#include <stddef.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int dum;
//- read in input file
//---------------------------------------------------------------------------------
// MODEL VALUE
dum=readvoid();
dum=readvoid();
#define Nx (readint()) // run on 64 nodes
#define Ny (readint()) // Y DIR
#define dx (readfloat()) // Grid Spacing
#define dt (readfloat()) // Time step
#define Max_Time (readint()) // Max run iterations
printf("model: %d,%d,%f,%f,%d\n",Nx,Ny,dx,dt,Max_Time);
printf("model: %d,%d,%f,%f,%d\n",Nx,Ny,dx,dt,Max_Time);
printf("model: %d,%d,%f,%f,%d\n",Nx,Ny,dx,dt,Max_Time);
return 0;
}
With the first printf working just fine and the others outputting random values.
What am I doing wrong? I am new to c but have experience in fortran, shell, python, matlab and am a bit lost.

Of course the responses are "gibberish". my_int is not initialized, so if sscanf does not read a value, then readint is returning an uninitialized variable. (aka, "gibberish"). Since you've already read the file and not rewound, getline is (probably) returning -1 and sscanf is not assigning a value. But you can't tell, because your program does not check the values returned by either getline or sscanf.
You need to rewind the stream if you want to read it again.
Also, you cannot interleave the calls to readint and readfloat in the parameters list of printf. The order in which they will be called is not fixed.

I found out the problem, thanks to above comments:
I naively thought that #define was able to read the output of e.g. readint() and store it as an object-like macro (e.g. #define Nx 500), but apparently this is not the case.
Thanks everyone!

Related

How to create 2D array from text file and print to new file in c

Wondering if I could get some advice. Firstly, I am very new to programming, so I apologise for any silly mistakes. Please feel free to point them out and I will try to go research to improve.
I feel I am doing something fundamentally wrong with my array.
I am trying to read in from a file whose filename is specified by user input, store the information from the file in a 2D array, which I then plan to print into another file, again defined by user input.
I am currently printing out the array, to check that it has been stored, but I believe I am using the 2D array incorrectly, as when I try to fprintf into my file, it just does not work.
Any advice would be greatly appreciated.
Thank you. Code as follows:
#include <stdio.h>
#include <string.h>
int main()
{
char finame[100];
printf("Enter file you would like to open: ");
scanf("%s", finame);
FILE *in = fopen(finame, "r");
char foname[100];
printf("Enter the filename you would like the output included in: ");
scanf("%s", foname);
FILE *out = fopen(foname, "w");
/*Char array to store string */
char str[50][20];
int i =0;
/*Loop for reading the file till end*/
while((fgets(str[i],sizeof(str[i]), in)) != NULL) {
fputs(str[i++],stdout);
//getchar();
}
return 0;
}
Avoid mixing fgets(), scanf() calls. scanf() leaves a newline character in the input buffer which is later consumed by fgets() (doesn't
matter in this case since input comes from a file not from stdin but a good practice overall).
There is also no protection for overflow, if you want to stick to scanf() add a width specifier and check the result to see if it succeeded.
if (scanf("%99s", finame) != 1) /* save one byte for \0 */
{
/* handle error case */
}
Check that you don't exceed the size of your array while writing to it.
Added 2 define directives that could clean your code up.
#define MAX_LINES 50
#define MAX_CHAR 20
char str[MAX_LINES][MAX_CHAR];
int i = 0;
while (i < MAX_LINES && (fgets(str[i], MAX_CHAR, in)) != NULL) /* always check if running out bounds */
{
fputs(str[i++], stdout);
}
The problem with the above code is that, if the file gets too big, you will end up missing data, what you could do is have a dynamic array and use a malloc / realloc approach to expand the array or a linked list.

Using fgets for file read

I'm new to using strings in C and am needing to read from a file lines of data that contain strings and numbers, parsing them as I go along. I've done similar programs reading in just numbers, such as a list of ordered pairs, using a for loop so this is the strategy I am leaning towards.
Example of data line in the file: PART,2.000,-1,0.050,V
When I compile I get an error in the for loop declaration of "expected expression before 'char'". What is missing or needs reviewing in this code?
#define flush fflush(stdin)
#define N 50
int main()
{
flush;
const char part[] = "PART"; // String for PART variable
char descriptor[N]; // Starting string of data set
double p_dim[N]; // Array for part dimensions
int t_sens[N]; // Array for sensitivity values: -1 or +1
double t[N]; // Array for part tolerance, bilateral
char t_status[N]; // Array for tolerance status, F(ixed) or V(ariable)
double key_max; // Maximum value of key characteristic
double key_min; // Minimum value of key characteristic
FILE* fpin;
if((fpin = fopen("input.txt","r"))==(FILE*)NULL)
{
printf("File input does not exist\n"); exit(-1);
}
// For loop to parse data lines from file
for(N; char* fgets(descriptor, int N, FILE* fpin); N-1);
{
compare(descriptor, part);
if (descriptor == part)
{
fscanf(fpin, "%lf,%d,%lf,%s", p_dim[N], t_sens[N], t[N], t_status[N]);
}
else if (descriptor != part)
{
fscanf(fpin, "%lf, %lf", &key_min, &key_max);
}
}
1.) #define flush fflush(stdin)
Flushing stdin invokes undefined behaviour.
2.) if((fpin = fopen("input.txt","r"))==(FILE*)NULL)
The cast to (FILE*) is superfluous.
3.) for(N; ... ; N-1);
You defined N as a constant (#define N 50) so this loop won't ever exit.
4.) for(... ; char* fgets(descriptor, int N, FILE* fpin); ...);
This is just plain wrong ...
I'd lean more toward breaking the string apart
See question 3501338 for reading a file line by line
See question 15472299 using strtok to break apart the string
If you need to cast the strings as numbers use sscanf

Reading first 5 characters from a file using fread function in C

How do i read some 5 to 10 characters from a sample txt file using an fread funtion.
I have the following code:
#include <stdio.h>
main()
{
char ch,fname[20];
FILE *fp;
printf("enter the name of the file:\t");
gets(fname);
fp=fopen(fname,"r");
while(fread(&ch,1,1,fp)!=0)
fwrite(&ch,1,1,stdout);
fclose(fp);
}
when i enter any sample filename..it prints all the data of the file.
my question is how to print only the first 5 to 10 characters from the sample file.
Your while loop runs until read reaches the end of the file (reads 0 bytes for the first time).
You will want to change the condition by using a for loop or a counter.
i.e. (these are suggestions, not the full working code):
int counter = 10;
while(fread(&ch,1,1,fp)!=0 && --counter)
fwrite(&ch,1,1,stdout);
or
int i;
for(i=0; i < 10 && fread(&ch,1,1,fp) > 0 ; i++)
fwrite(&ch,1,1,stdout);
Good luck!
P.S.
To answer your question in the comments, fread allows us to read the data in "atomic units", so that if a whole unit isn't available, no data will be read.
A single byte is the smallest unit (1), and you are reading one unite (of a single byte), this is the 1,1 part in the fread(&ch,1,1,fp).
You could read 10 units using fread(&ch,1,10,fp) or read all the bytes unrequited for a single binary int (this won't be portable - it's just a demo) using int i; fread(&i,sizeof(int),1,fp);
read more here.
Here is a modified version of your code. Check the comments at the lines that are modified
#include <stdio.h>
#define N_CHARS 10 // define the desired buffer size once for code maintenability
int main() // main function should return int
{
char ch[N_CHARS + 1], fname[20]; // create a buffer with enough size for N_CHARS chars and the null terminating char
FILE *fp;
printf("enter the name of the file:\t");
scanf("%20s", fname); // get a string with max 20 chars from stdin
fp=fopen(fname,"r");
if (fread(ch,1,N_CHARS,fp)==N_CHARS) { // check that the desired number of chars was read
ch[N_CHARS] = '\0'; // null terminate before printing
puts(ch); // print a string to stdout and a line feed after
}
fclose(fp);
}

Program crashes after perfoming strcpy() function

I need some help with an assignement I have to do for school which consists in sorting some books after the title,author and the publication date of it. All the infos are given as a string in a txt file using a delimiter between them. The problem is that I don't manage to properly read the data, my program crashes after I try to perform a strcpy(). Can you guys help me with this and tell me what have I done wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book
{
char author[100],title[100];
int year;
};
int main()
{
struct book b[25];
int i,n;
char intro[25][150],*p;
const char delim[2] = "#";
FILE *fp;
fp=fopen("text.txt", "r");
fscanf(fp,"%d",&n);
for(i=0;i<=n;i++)
{
fgets(intro[i], sizeof (intro[i]), fp);
p=strtok(intro[i], delim);
strcpy(b[i].title,p);
p=strtok(NULL, delim);
strcpy(b[i].author,p); /// The program works until it reaches this point - after performing this strcpy() it crashes
if(p!=NULL)
{
p=strtok(NULL,delim);
b[i].year=atoi(p);
}
}
return 0;
}
An example of input could be this:
5
Lord Of The Rings#JRR Tolkien#2003
Emotional Intelligence#Daniel Goleman#1977
Harry Potter#JK Rowling#1997
The Foundation#Isaac Asimov#1952
Dune#Frank Herbert#1965
The problem is with the newline left in the file after the initial fscanf() call.
This
fscanf(fp,"%d",&n);
reads the 5 and the subsequent fgets() reads just the \n. So this is not what you want. Read n using fgets() and convert it into integer using sscanf() or strto*. For example, instead of the fscanf() call, you can do:
char str[256];
fgets(str, sizeof str, fp);
sscanf(str, "%d", &n);
to read the n from file.
You should also check if strtok() returns NULL. If you did, you would have figured out the issue easily.
Also, your need to go from 0 to n-1. So the condition in the for loop is wrong. It should be
for(i=0; i<n; i++)

Read Magic Number from .au File

I wrote a small program to get the magic number from an .au file and print it to console, but every time I try, instead of getting the intended .snd, I get .snd$ instead.
I'm not sure why this is happening, considering that I'm only reading in 4 bytes, which is what the magic number is comprised of. So, where is the extra character coming from?
#include <stdio.H>
int main()
{
FILE *fin;
int r;
char m[4], path[20];
scanf("%s", path);
fin = fopen(path, "r");
r = fread(&m, sizeof(char), 4, fin);
printf("magic number is %s\n", m);
return 0;
}
You're printing it as though it were a string, which in C, means that it's NUL-terminated. Change your code like this and it will work as you expect:
char m[5];
m[4] = '\0'; /* add terminating NUL */
Also, you should be aware that scanf is a dangerous function. Use a command line argument instead.
The problem is not how you are reading.
The problem is that your variable is only 4 chars length, and it needs a null character to indicate the end.
printf with %s will print the content of the variable until reach a null character, until that it can print garbage if your variable is not correctly ended.
To fix you can have a bigger variable and set the [4] char with null.
How the new code should look like:
#include <stdio.H>
int main()
{
FILE *fin;
int r;
char m[5], path[20];
scanf("%s", path);
/*Scanf can be dangerous because it can cause buffer overflow,
it means that you can fill your variable with more bytes than it supports, which can end up being used for buffer overflow attacks:
See more: http://en.wikipedia.org/wiki/Buffer_overflow */
fin = fopen(path, "r");
r = fread(&m, sizeof(char), 4, fin);
m[4] = '\0';
printf("magic number is %s\n", m);
return 0;
}

Resources