Forcing users to write input in a specific format [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
i'm making a program in C. that keeps track of items in a warehouse.
and i want to force the user to include at least one number!
example dvd1, dvd2. hello1, hello20
is there a way to do this?
at this moment i am using scanf.
and i want the product code to have the requirement format of xx-xxx-xxx were x are numbers.
i'm using scanf ( %[0-9-]s
Mvh Anton!

scanf doesn't work like that, it doesn't have in-depth validation.
You need to read the input into a char array, then loop through each character and see if it is a digit.
Something like this (untested):
char buffer[1000];
int i = 0, hasDigit = 0;
scanf("%s", buffer);
while (i < sizeof(buffer) && buffer[i] != 0 && !hasDigit)
{
hasDigit = isdigit(buffer[i]);
i++;
}
// if hasDigit is 0, there are no digits
Note: scanf isn't great, since if you enter more characters than fit in the buffer it can cause a buffer overflow. It is better to use fgets(buffer, sizeof(buffer), stdin);

Read the input and you can iterate through as in This SO question. You can check to see if chars match the input you want pretty easily from that point on.

Related

My Strlen Syntax Not Written Well? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
if I write more than 20 charcters in my program it runs through it and skips my if statement if(length > 20). What did I do wrong?
printf("\nEnter Your Product:");
fgets(item_name, 20, stdin);
length = strlen(item_name);
if(length > 20){
Errorlevel("Input Greater Than 20");
}
You set fgets to gather a maximum of 20 chars. Thus, if(length > 20) is always false.
See documentation about fgets
The fgets will read up the the requested number of character minus one. The size you pass to the fgets function includes the terminator.
So no matter how many characters you write, the length will never be longer than 19 characters.
A simple way to check if to many characters were input is to see if the last character (not the string terminator) is a newline or not. If it's not then 19 or more characters were entered as input.

How to use read() and/or fgetc to get certain strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using fgetc() and/or read() how to i get the keys and the values from a file?
This is how it is written in the file:
<key1,val1><key2,val2><key3,val3>
And i need to put each key and value in a new array(to do linked-lists).
So, to insert in the file using '<' and '>' and ',' it's no problem. The problem is how to get them?
Any ideas?
Here's what I have.
FILE * file;
char* key;
char* value;
int c;//apparently I have to use an int
file=fopen("myfile.txt", "r");
c=fgetc(file);
while(c != EOF)
{
c=fgetc(file);
if(c == '<'){
for(int i=0; c != ','; i++)
{
key[i]=fgetc(file);
}
}
}
This is till the first comma. Now I lack a little of algorithm conception. Help please.
Does it have to be fgetc and read ? .. Here is one way to do this :
while(!feof(your_file_name)){
bzero(line, sizeof(line)); // line is a char array ... make sure its of sufficiently large size
fgets(line,100,your_file_name); // I am reading 100 characters .. you can change that number as per your file accordingly
}
fclose(your_file_name);
sscanf(line,"<%d,%d><%d,%d><%d,%d>",&key1,&val1,&key2,&val2,&key3,&val3); // assuming keys and vals are ints
What you are doing in your code is reading the file character by character .. Why go through all that pain?
using fgets will read the entire file in one go ... then knowing the format of the file sscanf is immensely powerful tool to recover info from that
Ok so you posted in comments that keys and vals are strings .. so change the sscanf to this:
sscanf(line,"<%[^,]%[^>]<%[^,]%[^>]<%[^,]%[^>]",key1,val1,key2,val2,key3,val3);
Ok so let me explain
When dealing with the strings in sscanf you have to specify the delimiting character.
So this <%[^,] tells sscanf - start reading a string from after this < character and read it until you reach , character ...

How to print a huge string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm a beginner and I need to know how to print an entire help page in C.
I am trying:
unsigned short * entireHelpPage;
unsigned int * someString:
printf("comparing %s to %s", someString, entireHelpPage);
this is printing something like this :
comparing Dog to Dog is a domestic animal.. blah blah.. Dogs are bred in mos
As you will see that entireHelpPage is not getting displayed completely when I try to print it.
Please let me know how to get it to print the entire help page.
Use a loop to get around limitation in printf() or potential memory/display problem.
OP is experiencing some issue. printf() should be able to print at least 4095 characters before it has troubles. To get around a non-conforming issue, use a loop. To find unexpected non-printable characters, print them out in a special fashion.
const char *s = (const char *) entireHelpPage;
fputs(">", stdout);
while (*s) {
if (isgraph(*s)) {
fputc(*s, stdout);
}
else {
fprintf(stdout, "[%02X]", (unsigned) *s);
}
s++;
}
fputs("<\n", stdout);
Further: it is strange to use unsigned short * as a pointer to char* data. I suspect that the tailing memory pointer to by entireHelpPage is being unexpectedly over-written by code. It could be that entireHelpPage is a buffer about 400 bytes and is simple not larger enough for the help page.

printf struct from binary file to screen [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i am currently struggling about couldn't output(printf) or correctly read the binary.
The following code resulted as (fread: Success) though.
if (fread(list, sizeof(struct Ticket), 10, fp) == 10){
perror("fread");
}
then i try to printf with the following code:
for ( i = 0; i < 10; i++ ) {
printf("%d\t%s\t%d\n", list[i].code, list[i].station, list[i].price);
}
Resulted messy unidentify code(although it was 10 line).
also, when i try to go forward with following code:
printf("\nPlease submit your destination\n");
scanf("%d\n", dec);
fprintf(stdout, "%d\t%s\t%d\n", list[dec-1].code, list[dec-1].station, list[dec-1].price);
Error appeared as Segmentation violation after input at scanf.
P.S. The fwrite was tested with perror("fwrite");
and resulted success.
Sorry for poor english and programming.
would really appreciate if anyone could help.
Thanks
If you want to print formatted output,use fscanf instead of fread.
fscanf() is used for reading from text files (i.e. readable by humans)
fread is used for reading from binary files (i.e. in formats used
by computers internally).
And you are getting segmentation violation since you have written scanf in wrong way.It should be
scanf("%d\n", &dec);
You are printing output on screen using format specifiers.Then you should read your data using format specifier.No doubt you can read any file using fread
You can read use of fread here

atoi() returning 0 from char array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a program in which stdin is read into a buffer, then processed. The vast majority of these items that need to be processed are strings (or well, character arrays). However, I do have one item that needs to be read in as a character array and then converted to int for ease of use in the future.
for(i=0; i<n; i++){
num[i] = buff[(i)];
printf("%c", num[i]);
}
convert = atoi(num);
So I know for sure that the correct group of characters is being read into num because the printf for that is correct. However, when I try to print convert I end up getting 0, and I'm very perplexed as to what I'm doing wrong. I know that the 0 return means that a valid conversion could not be performed, but I don't know what's making it invalid. Any tips?
EDIT: Sorry for not including these before >_<
n is the number of chars in the buff array
buff is the buffer array stdin is read into
atoi is a function that gives you no means to analyze error conditions. On top of that, it produces undefined behavior in overflow situations. Don't ever use atoi (or atof or anything from ato... group) in real-life programs. It is practically useless.
To perform string-to-number conversions use strtol (and other functions from strto... group).
Now, what is inside your num at the moment you call your atoi? Is your num properly zero-terminated?

Resources