I am trying to write a programme in C for an assignment that detects CSV format eg. decimalcommadecimal. and gives a output as to if the file is in the required format or not. I have tried using various inputs from stdin and using isdigit etc. but to no success. I’m a mega noob and have barely done any C programming before, I attempted to use regexc but couldn’t figure out the syntax for using it.
#include <ctype.h>
#include <stdio.h>
const char EOL = '\n';
int cbreak(void);
int check_dig(void);
int value =1;
char c;
int main()
{
while((scanf("%c" ,&c)) !=EOF&& value !=0)
check_dig();
printf("\n%d\n",value);
}
int check_dig()
{
if (c == EOL)
scanf("%c", &c);
if (c == isdigit)
scanf("%c", &c);
else if (c == ',')
scanf("%c", &c);
else value = 0;
}
Thanks Guys I’m now to this stage but stumped as how to finish, I need to printf either 1 or 0 depending on validation and I want to do this as suggested using a return value.
#include <ctype.h>
#include <stdio.h>
int check_digit(int);
int check_comma(int);
int skip_char(int);
int main()
{
int c;
while ((c = getchar()) !=EOF)
if (check_digit(c))
skip_char(c);
else if (check_comma(c))
skip_char(c);
else return 0;
}
int check_digit(int c)
{
if (isdigit(c))
return 1;
else return 0;
}
int check_comma(int c)
{
if (c == ',')
return 1;
else return 0;
}
int skip_char(int c)
{
c = getchar(); // will this skip 2 chars as i have a while loop that has c=getchar()??
return c;
}
First off I’d recommend to not use that vast list of globals. Use parameters in functions and return values from functions.
Second
isdigitdoes not work like that. It takes a parameter and return true or false. isdigitFurther I’d use getchar over scanf.
Your
int check_dig()function is a bit strange. You keep reading characters inside the function.I would perhaps do something like:
Edit: As a rule of thumb. A function should do one thing, and one thing only, and do it well. The name of the function should reflect what it does.
Edit.2:
You do not need to “skip char”. The way it goes, in your new code, is that you skip every other character.
I.e.:
File: 12,33,66,14
In your code you’ll get
Further; I know I wrote “a function should do one thing” – but but not that literal. Ie your new
check_digitis redundant. Useisdigitdirectly. If you have floats in your csv you’ll have to expand or use a different approach.To illustrate by example; easier then writing on here 🙂
Edit.3:
Code structure is something one has to learn just as much as the language itself. It is learning by doing and writing. That one realize that re-structure of code is necessary happens from time to time – but the more one think before writing, make a simple structure at first and expand it, etc. it is more avoidable.
Anyhow; practice, practice, practice. And keep these topics in mind at all time.
Even if it can “look” simple, it is not. I think often books, tutorials, courses etc. have all to little focus on this topic. It is all about
for,if, functions etc. and all to little on how to stitch it all together in a good way.There are several advantages of splitting up the code.
The last point is actually a way I often think of it when writing; “How should I best implement this code so that, if, in the future, I’d like to expand it to cover more scenarios, it can be easily done.”
Myself I use this as a base when writing in C combined with things I’ve learned in K & R’s ANSI C book ++. See for example what is written about functions.
Also; being strict on coding style makes it much easier to read and maintain. I use in large part what is described in the document above. It is no law, but being conscious about it makes the coding life so much simpler.