I am working on my school assignment and I've come across my first major hurdle... I am unable to write to a file or even retrieve input using scanf and fgets.
Problem 1:
FILE *f1;
char date_trans[100][15];
f1 = fopen("test.txt", "w");
if (f1 == NULL)
printf("File could not be opened.\n");
else
printf("Enter details (sender, receiver, amount.)\n");
for(i = 0; i <= element; i++)
{
for(j = 0; j <= 20; j++)
fgets(date_trans[i][j], 15, stdin);
printf("%s", date_trans[i][j]);
}
fclose(f1);
This is a small snippet; with the above code I am unable to retrieve the input entered and print it out on display/to a file.
My intent is to have 100 strings and up to 15 characters each.
So say if I were to enter a date like 18/12/15 or like 18-12-15, or a word like "Daniel", the program stops abruptly for a few seconds after I press enter and then proceeds to display "Press any key...".
I also receive this error...
[Warning] ...\assignment\test.c:22: warning: passing arg 1 of `fgets' makes pointer from integer without a cast
which I really do not understand as the variable date_trans is a string/an 2d array of characters, not an integer, right?
Edit:
Problem 2:
for (j=0; j <= 50 || !feof(f1); j++); // f1 opened in 'r' mode.
{
fscanf(f1, "%s %d %s %d %s %f", date_trans[j], &accsend[j], accnames[j],
&accreceive[j], accnamer[j], &amount_trans[j]);
printf("%d %s %d %s %d %s %.2f\n", j+1, date_trans[j], accsend[j], accnames[j],
accreceive[j], accnamer[j], amount_trans[j]);
}
In the code above, I am trying to input data through the stream f1 with function fscanf, which was opened in read mode. However the output makes the program hang and I am also unable to type or do any actions within the program.
As well, please advise whether my usage of !feof is correct/appropriate.
Thanks for any help.
I see two problems with the code you show:
The warning you get, because date_trans[i][j] is a char, not a char *
Your inner loop goes from 0 to 20 (inclusive) and you use that as index into an array with 15 elements (i.e. indexes from 0 to 14 inclusive).
To solve both problems, don't have the inner loop, instead in the outer do
fgets(data_trans[i], sizeof data_trans[i], stdin);
Problem number 1 also exists in the printf call, where you pass a single char to a format that expects a char *.
Lastly a note about your requirement of strings containing 15 characters: Don't forget the null-terminator, and its need for a space in the array as well. That means you currently have place for 14 characters plus the terminator.
Change this
fgets(date_trans[i][j], 15, stdin);
printf("%s", date_trans[i][j]);
to
fgets(date_trans[i], 15, stdin);
printf("%s", date_trans[i]);
date_trans[i][j] is char type not char *. Also you don't need j for loop.
Related
I have an exercise in C. I wrote a function
int readArray(FILE *wp, char *name, int n, double M[n][n])
{
fscanf(wp, "%s", &name[0]);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
fscanf(wp, "%lf", &M[i][j]);
return 1;
}
How can I change this? I have the following warning
warning: fscanf() without field width limits can crash with huge input data. [invalidscanf]
fscanf(wp, "%s", &name[0]);
The %s placeholder is used to read a word into a string.
By default, there is no restriction on the length of that word. A field width specifier can be used together with the %s placeholder to limit the number of bytes which will by written to the buffer.
So you should do something like:
char name[100];
fscanf(wp, "%99s", name);
For example, if the length of name that you get from file is greater than 100, this function will overflow. The problem is similar to scanf also.
It's not an error. It's a warning.
And you solve it by simply adding a limit to the field width, like this:
fscanf(wp, "%1234s", &name[0])
The value 1234 is just made up. The above code will read at most 1234 bytes.
The message is warning you that that fscanf has no control on the input size, making possible to write data beyond target buffer size. In order to solve it just apply the length to %s format:
char name[50];
fscanf(wp, "%49s", &name[0]);
This makes sure that at most 49 characters are stored into name array (that in my example has size 50. One character has to be left to the string terminator).
so in my CS course we have to make a calculator which reads input and then calculates the result after the = sign has been read.
Input always consists of one number followed by operator.
I'm now struggling with the way of scanning the input. I want to use some loop which would always store it like:
scanf("%lf %s ", &in, c);
Note: the calculator goes one operation after other. Thus in example below the plus sign works only for 20 and 4, and only then the result takes the division sign and gets divided by 8. Then the new result gets negated by 'neg' string.
E.g.: Input: 20 + 4 / 8 neg =
Output: Result: -3
This is how I tried to solve it. My intention was to make the loop store each number into a new "box" of array, and then to store each string into the new "line" of char array. The 4 in op array is meant to set the max length of the string, because i know that the longest string that can occur is "sqrt".
I see that the '\n' in the condition of for is probably useless but can't figure out how to do it...
I'm thinking about using either for, or a while loop.
int main(){
double in[10];
char op[5][4];
for(int i=0; i<5;i++){
scanf("%lf %s ", &in[i], op[i][4]);
}
printf("%.3f %s", in[0], op[0][0]);
return 0;
}
//just a "skeleton" of the code. There's more to it, but here I submitted just the part that I'm struggling with.
For example if I run this code, I want to write a few numbers followed by operator into the input.
I expect it to print the first number and string (just to check whether the loop works).
But actually it does absolutely nothing and just gives me some large negative number as a return.
From the man page of scanf
int scanf(const char *format, ...);
as you can see first argument is of const char * type i.e you need to provide valid address.
With this
char op[5][4]; /* 5 char array, in each char array you can have 4 char's
* i.e op[row][0] to op[row][3]. There is no op[i][4]
*/
you can have
for(int i=0; i<5;i++){
scanf("%s",op[i]); /* op[i] itself address, so don't need & */
}
or
for(int i=0; i<5;i++){
for(int j=0; j<4; j++) {
scanf(" %c", &op[i][j]); /* storing char by char */
}
}
Also while printing here use %c as op[0][0] is of char type.
printf("%.3f %c", in[0], op[0][0]);
In a scanf format string, %s indicates you want to read a string, meaning it needs the address of where to put that string. You are passing op[i][4] for it, which is a char.
My simple program is read from the text file one word at a time.
example text file. (below)
N 101001 Circular Queue 11 1
N 123456 List Linker 11 5
N 666666 Pseudocode Gen 38 3
N 110010 Stack Stretcher 3 2
and simple codes including variables etc...
char function;
int number, init_stock, reorder;
char info[100];
FILE *fp;
if((fp = fopen("input.txt", "r")) == NULL)
{
printf("input open failed\n");
return 0;
}
while(!feof(fp))
{
fscanf(fp, "%c %d %[A-z, ] %d %d ", &function, &number, info, &init_stock, &reorder);
}
When a variable was changed within the ' while loop',
I expected to affect it the next loop as well.
so.. first, 'Circular Queue' was stored in 'info'.
Second, when 'List Linker' was stored in 'info' because second string is shorter than first string,
I thought that the info array had string like this 'List Linkerue'.
info -- Circular Queue
info -- List Linker+ue
But the info seemed to be reset every time, and I do not know why.
On the first pass the content of info is "Circular Queue \0" The \0 at the end represents a single zero byte, it is the null character. This tells functions like printf where string ends.
Note that there is also a blank space at the end.
On the second pass the content of info is "List Linker \0e". As you suspected, fscanf didn't fill the whole string. But it did add the null character, so you can't see the e at the end. Content of info will be
1st pass: "Circular Queue " ... followed by random bytes
2nd pass: "List Linker \0e "
You can also improve your function by changing the format to %99[A-z, ] because info is 100 bytes (including the null character) And make sure fscanf reads 5 values as expected. You might also consider changing the input so the values are separated by commas.
while(fscanf(fp, "%c %d %99[A-z, ] %d %d ",
&function, &number, info, &init_stock, &reorder) == 5)
{
//optional: remove the extra space at the end of info
int len = strlen(info);
if(len)
if(info[len - 1] == ' ')
info[len - 1] = 0;
printf("string: [%s] %d\n", info, number);
}
My problem is when I try to save the string (series[0]) Inside (c[0])
and I display it, it always ignore the last digit.
For Example the value of (series[0]) = "1-620"
So I save this value inside (c[0])
and ask the program to display (c[0]), it displays "1-62" and ignores the last digit which is "0". How can I solve this?
This is my code:
#include <stdio.h>
int main(void)
{
int price[20],i=0,comic,j=0;
char name,id,book[20],els[20],*series[20],*c[20];
FILE *rent= fopen("read.txt","r");
while(!feof(rent))
{
fscanf(rent,"%s%s%s%d",&book[i],&els[i],&series[i],&price[i]);
printf("1.%s %s %s %d",&book[i],&els[i],&series[i],price[i]);
i++;
}
c[0]=series[0];
printf("\n%s",&c[0]);
return 0;
}
The use of fscanf and printf is wrong :
fscanf(rent,"%s%s%s%d",&book[i],&els[i],&series[i],&price[i]);
Should be:
fscanf(rent,"%c%c%s%d",&book[i],&els[i],series[i],&price[i]);
You have used the reference operator on a char pointer when scanf expecting a char pointer, also you read a string to book and else instead of one character.
printf("1.%s %s %s %d",&book[i],&els[i],&series[i],price[i]);
Should be:
printf("1.%c %c %s %d",book[i],els[i],series[i],price[i]);
And:
printf("\n%s",&c[0]);
Should be:
printf("\n%s",c[0]);
c is an array of char * so c[i] can point to a string and that is what you want to send to printf function.
*Keep in mind that you have to allocate (using malloc) a place in memory for all the strings you read before sending them to scanf:
e.g:
c[0] = (char*)malloc(sizeof(char)*lengthOfString+1);
and only after this you can read characters in to it.
or you can use a fixed size double character array:
c[10][20];
Now c is an array of 20 strings that can be up to 9 characters long.
Amongst other problems, at the end you have:
printf("\n%s",&c[0]);
There are multiple problems there. The serious one is that c[0] is a char *, so you're passing the address of a char * — a char ** — to printf() but the %s format expects a char *. The minor problem is that you should terminate lines of output with newline.
In general, you have a mess with your memory allocation. You haven't allocated space for char *series[20] pointers to point at, so you get undefined behaviour when you use it.
You need to make sure you've allocated enough space to store the data, and it is fairly clear that you have not done that. One minor difficulty is working out what the data looks like, but it seems to be a series of lines each with 3 words and 1 number. This code does that job a bit more reliably:
#include <stdio.h>
int main(void)
{
int price[20];
int i;
char book[20][32];
char els[20][32];
char series[20][20];
const char filename[] = "read.txt";
FILE *rent = fopen(filename, "r");
if (rent == 0)
{
fprintf(stderr, "Failed to open file '%s' for reading\n", filename);
return 1;
}
for (i = 0; i < 20; i++)
{
if (fscanf(rent, "%31s%31s%19s%d", book[i], els[i], series[i], &price[i]) != 4)
break;
printf("%d. %s %s %s %d\n", i, book[i], els[i], series[i], price[i]);
}
printf("%d titles read\n", i);
fclose(rent);
return 0;
}
There are endless ways this could be tweaked, but as written, it ensures no overflow of the buffers (by the counting loop and input conversion specifications including the length), detects when there is an I/O problem or EOF, and prints data with newlines at the end of the line. It checks and reports if it fails to open the file (including the name of the file — very important when the name isn't hard-coded and a good idea even when it is), and closes the file before exiting.
Since you didn't provide any data, I created some random data:
Tixrpsywuqpgdyc Yeiasuldknhxkghfpgvl 1-967 8944
Guxmuvtadlggwjvpwqpu Sosnaqwvrbvud 1-595 3536
Supdaltswctxrbaodmerben Oedxjwnwxlcvpwgwfiopmpavseirb 1-220 9698
Hujpaffaocnr Teagmuethvinxxvs 1-917 9742
Daojgyzfjwzvqjrpgp Vigudvipdlbjkqjm 1-424 4206
Sebuhzgsqpyidpquzjxswbccqbruqf Vuhssjvcjjylcevcisdzedkzlp 1-581 3451
Doeraxdmyqcbbzyp Litbetmttcgfldbhqqfdxqi 1-221 2485
Raqqctfdlhrmhtzusntvgbvotpk Iowdcqlwgljwlfvwhfmw 1-367 3505
Kooqkvabwemxoocjfaa Hicgkztiqvqdjjx 1-466 435
Lowywyzzkkrazfyjuggidsqfvzzqb Qiginniroivqymgseushahzlrywe 1-704 5514
The output from the code above on that data is:
0. Tixrpsywuqpgdyc Yeiasuldknhxkghfpgvl 1-967 8944
1. Guxmuvtadlggwjvpwqpu Sosnaqwvrbvud 1-595 3536
2. Supdaltswctxrbaodmerben Oedxjwnwxlcvpwgwfiopmpavseirb 1-220 9698
3. Hujpaffaocnr Teagmuethvinxxvs 1-917 9742
4. Daojgyzfjwzvqjrpgp Vigudvipdlbjkqjm 1-424 4206
5. Sebuhzgsqpyidpquzjxswbccqbruqf Vuhssjvcjjylcevcisdzedkzlp 1-581 3451
6. Doeraxdmyqcbbzyp Litbetmttcgfldbhqqfdxqi 1-221 2485
7. Raqqctfdlhrmhtzusntvgbvotpk Iowdcqlwgljwlfvwhfmw 1-367 3505
8. Kooqkvabwemxoocjfaa Hicgkztiqvqdjjx 1-466 435
9. Lowywyzzkkrazfyjuggidsqfvzzqb Qiginniroivqymgseushahzlrywe 1-704 5514
10 titles read
I am getting the user to input 4 numbers. They can be input: 1 2 3 4 or 1234 or 1 2 34 , etc. I am currently using
int array[4];
scanf("%1x%1x%1x%1x", &array[0], &array[1], &array[2], &array[3]);
However, I want to display an error if the user inputs too many numbers: 12345 or 1 2 3 4 5 or 1 2 345 , etc.
How can I do this?
I am very new to C, so please explain as much as possible.
//
Thanks for your help.
What I have now tried to do is:
char line[101];
printf("Please input);
fgets(line, 101, stdin);
if (strlen(line)>5)
{
printf("Input is too large");
}
else
{
array[0]=line[0]-'0'; array[1]=line[1]-'0'; array[2]=line[2]-'0'; array[3]=line[3]-'0';
printf("%d%d%d%d", array[0], array[1], array[2], array[3]);
}
Is this a sensible and acceptable way? It compiles and appears to work on Visual Studios. Will it compile and run on C?
OP is on the right track, but needs adjust to deal with errors.
The current approach, using scanf() can be used to detect problems, but not well recover. Instead, use a fgets()/sscanf() combination.
char line[101];
if (fgets(line, sizeof line, stdin) == NULL) HandleEOForIOError();
unsigned arr[4];
int ch;
int cnt = sscanf(line, "%1x%1x%1x%1x %c", &arr[0], &arr[1], &arr[2],&arr[3],&ch);
if (cnt == 4) JustRight();
if (cnt < 4) Handle_TooFew();
if (cnt > 4) Handle_TooMany(); // cnt == 5
ch catches any lurking non-whitespace char after the 4 numbers.
Use %1u if looking for 1 decimal digit into an unsigned.
Use %1d if looking for 1 decimal digit into an int.
OP 2nd approach array[0]=line[0]-'0'; ..., is not bad, but has some shortcomings. It does not perform good error checking (non-numeric) nor handles hexadecimal numbers like the first. Further, it does not allow for leading or interspersed spaces.
Your question might be operating system specific. I am assuming it could be Linux.
You could first read an entire line with getline(3) (or readline(3), or even fgets(3) if you accept to set an upper limit to your input line size) then parse that line (e.g. with sscanf(3) and use the %n format specifier). Don't forget to test the result of sscanf (the number of read items).
So perhaps something like
int a=0,b=0,c=0,d=0;
char* line=NULL;
size_t linesize=0;
int lastpos= -1;
ssize_t linelen=getline(&line,&linesize,stdin);
if (linelen<0) { perror("getline"); exit(EXIT_FAILURE); };
int nbscanned=sscanf(line," %1d%1d%1d%1d %n", &a,&b,&c,&d,&lastpos);
if (nbscanned>=4 && lastpos==linelen) {
// be happy
do_something_with(a,b,c,d);
}
else {
// be unhappy
fprintf(stderr, "wrong input line %s\n", line);
exit(EXIT_FAILURE);
}
free(line); line=NULL;
And once you have the entire line, you could parse it by other means like successive calls of strtol(3).
Then, the issue is what happens if the stdin has more than one line. I cannot guess what you want in that case. Maybe feof(3) is relevant.
I believe that my solution might not be Linux specific, but I don't know. It probably should work on Posix 2008 compliant operating systems.
Be careful about the result of sscanf when having a %n conversion specification. The man page tells that standards might be contradictory on that corner case.
If your operating system is not Posix compliant (e.g. Windows) then you should find another way. If you accept to limit line size to e.g. 128 you might code
char line[128];
memset (line, 0, sizeof(line));
fgets(line, sizeof(line), stdin);
ssize_t linelen = strlen(line);
then you do append the sscanf and following code from the previous (i.e. first) code chunk (but without the last line calling free(line)).
What you are trying to get is 4 digits with or without spaces between them. For that, you can take a string as input and then check that string character by character and count the number of digits(and spaces and other characters) in the string and perform the desired action/ display the required message.
You can't do that with scanf. Problem is, there are ways to make scanf search for something after the 4 numbers, but all of them will just sit there and wait for more user input if the user does NOT enter more. So you'd need to use gets() or fgets() and parse the string to do that.
It would probably be easier for you to change your program, so that you ask for one number at a time - then you ask 4 times, and you're done with it, so something along these lines, in pseudo code:
i = 0
while i < 4
ask for number
scanf number and save in array at index i
E.g
#include <stdio.h>
int main(void){
int array[4], ch;
size_t i, size = sizeof(array)/sizeof(*array);//4
i = 0;
while(i < size){
if(1!=scanf("%1x", &array[i])){
//printf("invalid input");
scanf("%*[^0123456789abcdefABCDEF]");//or "%*[^0-9A-Fa-f]"
} else {
++i;
}
}
if('\n' != (ch = getchar())){
printf("Extra input !\n");
scanf("%*[^\n]");//remove extra input
}
for(i=0;i<size;++i){
printf("%x", array[i]);
}
printf("\n");
return 0;
}