Probelm with fscanf in C [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Ok, so i've been having a probelm with using int and char for my function. And is getting an error message enter image description here want to know how i should fix this with the code that i have:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char name[100];
int roll_no, chars;
float marks;
fp = fopen("records.txt", "r");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fscanf() function: \n\n");
printf("Name:\t\tRoll\t\tMarks\n");
while( fscanf(fp, "Name: %s\t\tRoll no: %d\t\tMarks: %f\n"
, name, &roll_no, &marks) != EOF )
{
printf("%s\t\t%d\t\t%.2f\n", name, roll_no ,marks);
}
fclose(fp);
return 0;
}
The intedend output was this... enter image description here
Advice/help on how to use the char function at line 8 would be appericated

NOTE: This answer refers to revision 1 of the question. Meanwhile, OP has clarified the question and is asking for additional help concerning a related issue. That is why this answer does not fully address the question in its current state.
That's not an error message, that is simply a warning because you declare the variable 'chars' on line 8 and never use it in your program. Your program should be able to run even if there are compiler warnings, which are different from compiler errors.

Related

Is this some "Repetitive Error Loop in C?" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm witnessing some trouble compiling this code wherein you may see a nice if-else statement in practice but as soon as I compile the code, I get a "statement missing" error in the compiler to basically terminate the "if" statement before the highlighted parenthesis, which is weird but when I perform it, the compiler starts to show another error stating a "misplaced else" statement now.
What is the actual error in here and how should I proceed?
///////////////////// Input Code ////////////////////
int getcount()
{int count=0;
FILE*fp;
fp=fopen("counter.DAT","rb");
If(fp==NULL)
*{
fp=fopen("counter.DAT","wb");
count=2;
If(fp==NULL)
{
printf("\nErorr");
getch();
exit(0);
}
fwrite(&count,sizeof(int),1,fp);
count=1;
}
else
{
fread(&count,sizeof(int),1,fp);
fclose(fp);
fp=fopen("counter.DAT","wb");
If(fp==NULL)
{
printf("\nErorr");
getch();
exit(0);
}
count++;
fwrite(&count,sizeof(int),1,fp);
count--;
}
while(count>2)
{count=count-2;}
fclose(fp);
return count;}
Try a lower-case, "if"... I haven't tested it, but at first glance, that seems to be what's going on here.

How to read file in C programming using codeanywhere? [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
here is the code given to me and a have also a text file where i need to get the Text from and compile the program.
You need to use open a file with fopen() first. But the current user needs to have perms to read/write the file.
We will use r to only read from a file. If the file is not read it will return NULL. You can fscanf() function to get the value of a file. The second parameter represents the type of the variable as in this case it's a string(char), third param is the mem address of the variable itself. Kind of like file version of scanf().
int main()
{
char a[1000];
FILE *myFile;
if ((myFile = fopen("C:\\myUSER\\newprogram.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(myFile ,"%s", &a);
printf("Value of a=%s", a);
fclose(myFile);
return 0;
}

How to dynamically change filename while writing in a loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to open file in 20 loop. Every time the name of the folder changes.Like This variables1,variables2,variables3......variables20 I found the same question in here , but it didnt help me.
Here's what I have tried:
int l=1;
while(l<20){
char filename[10];
sprintf (filename, "variables%d", l);
OR
scanf("%s", filename);
FILE * fp;
if ((fp = fopen (filename,"rb")) == NULL){
printf("Failed to Open File variables%d\n",l);}
........... Reading Data........
fclose (fp);
l++;
}
I can wite Filename succesfully but I got the error: Failed to Open File variables1
[SOLVED] I am just sodding idiot.Thank you for your concern and answers... i just forgot to add ".bin" sprintf (filename, "variables%d.bin", l);
You never increment your counter.
I would also recommend you to use a for loop like this
for(int i = 1; i < 20; i++){
// Your code
}
Your filename buffer is too short - "variables1" requires 10 characters plus a '\0' terminator, so you need at least 11 characters for this buffer, and more when the index is > 9, otherwise you will get a buffer overflow and undefined behaviour. Change:
char filename[10];
to:
char filename[PATH_MAX]; // PATH_MAX is defined in <limits.h>
Also: if, as your title suggests, you want to write to these files, then you need to change:
if ((fp = fopen (filename,"rb")) == NULL){
to:
if ((fp = fopen (filename,"wb")) == NULL){

how we can read char* in Linux [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can we read datatype char* from the user in a pipe between two processes?
Here is my code:
#include<stdio.h>
#include<unistd.h>
#define MSGSIZE 120
int main (
{
char *msg ;
char inbuff[MSGSIZE];
int p[2];
int ret;
pipe(p);
ret = fork();
if(ret != 0){
close(p[0]);
printf("Please Enter the msg\n");
scanf("%s",msg);
printf("I am the parent sending a msg %s\n",msg);
write(p[1],msg,MSGSIZE);
}
else
{
sleep(10);
close(p[1]);
read(p[0],inbuff,MSGSIZE);
printf("\nI am the child with msg %s",inbuff);
}
return 0;
}
My problem is that whenever the compiler gets into scanf() I am getting an error segmentation fault
You need to define msg as follow:
char msg[MSGSIZE];
You are calling scanf(). You want to tell it to put the entered data somewhere. But you don't give it such a space.
Instead, you give it a pointer, which is uninitialized and therefore points to an unknown location. That leads to segfault.
You could use the inbuf instead.
But you should refrain from using scanf() in this way. If the user enters more data than your buffer can hold, weird things happen.
Instead, use fscanf().

Junk values from an input file in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have this bit of C code below. When I place printf statements to test the text from the input file, I see that I'm getting a bunch of junk values, to be more specific they are not even alphabetic or numerical, I think they are diamonds with question marks in them. I assume this means it is not processing these values the way it should be. The input file a bit of MIPS assembly code, but in this context it is only a text file. I have commented out all other parts of my program and am left with this small piece and yet I still receive the bad values. What could I possibly be doing wrong here?
The command I use to run the program on the console is:
./assembler -symbols adder.asm
Where ./assembler is the driver (argv[0])
-symbols is a tag used (argv[1])
adder.asm is the input file (argv[2])
So once opened I should be able to grab text out of this file, and it's not a problem with the file as far as I believe, it was working earlier.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
FILE *fp;
FILE *fp_out;
void main(int argc, char* argv[])
{
int mode;
if (strcmp(argv[1], "-symbols") == 0)
{
fp = fopen(argv[2], "r");
mode = 1;
}
else
{
fp = fopen(argv[1], "r");
fp_out = fopen(argv[2], "w");
mode = 2;
}
}
Try to add the following line right after the open section and add #include <errno.h> to the beginning.
printf("%p, %p, %d\n", fp, ftp_out, errno);
If the fp is null then there is some problem opening the file. If you do not check the return value, you can read from a wrong buffer. Maybe there is some permission problems (or whatever). Also if errno != 0 you have a problem. Check with perror <num> the errno value in command line (or see perror(3) function).

Resources