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 7 years ago.
Improve this question
I was asked to solve a programming challenge, and there is this line which I don't understand can some one explain to me how can I pass the test cases to the program using this command, I think I have to store it in some file but I am not sure
size_t getline(char **lineptr,size_t *n, FILE *stream);
here is the full code
#include <stdio.h>
#include <stdlib.h>
size_t getline(char **lineptr,size_t *n, FILE *stream);
int main()
{
size_t maxLineLen=1024;
char *line = (char*)malloc(maxLineLen);
while(getline(&line, &maxLineLen,stdin)!= -1){
printf("Hello, World!\n");
printf("%s\n",line);
}
}
Seems you are asking how to run the given code and get input into.
getline(&line, &maxLineLen,stdin)
That reads a line from stdin. stdin is a standard file stream and is opened by the startup code for you. Without redirection, reading from stdin will get the input typed into the terminal
So to get input into the program you can do one of the following:
Run the program and then type each input line into the terminal.
Run the program and then redirect a file into the program. Example:
./my_program < my_input.txt
Are you given a file name?
Then the FILE * parameter would have to be opened via fopen.
See manual for fopen
Related
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 3 years ago.
Improve this question
Where is the directory?
i've searched for possibly all the directories but couldn't find any file related to the program
In the current working directory. If it does not maybe the call is not successfully. Check its return-code.
fopen() function is used to open a file to perform operations such as reading, writing etc. In a C program, we declare a file pointer and use fopen() as below. fopen() function creates a new file if the mentioned file name does not exist. The directory where you run the program from
The following example shows the usage of fopen() function.
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * fp;
fp = fopen ("file.txt", "w+");
fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
fclose(fp);
return(0);
}
Let us compile and run the above program that will create a file file.txt with the following content −
We are in 2012
Now let us see the content of the above file using the following program −
#include <stdio.h>
int main () {
FILE *fp;
int c;
fp = fopen("file.txt","r");
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
Let us compile and run the above program to produce the following result −
We are in 2012
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;
}
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 6 years ago.
Improve this question
I have a file y.txt that contains the text abcdefgh
this code convert file descriptor(int fd) to file pointer (FILE*), and tries to read from it.
#include <stdio.h>
#include<fcntl.h>
int main()
{
FILE *fp;
int fd = open("y.txt",O_RDONLY);
printf("%d",fd);
fp=fdopen(fd,"r");
close(fd);
char a[5];
a[4]='\0';
fread(a,2,1,fp);
printf("%s",a);
return 0;
}
The program outputs p instead of ab, as it should if it is reading from the begining of y.txt.
What am I doing wrong?
I see a problem with your code. You close(fd) before you are done with fp, which is probably causing your calls to fread to fail. This is because when you use fdopen:
The file descriptor is not dup'ed.
meaning that the underlying file that fp is trying to read from gets closed when you run close(fd). To fix this, you can remove the call to close and add a call to fclose which should close fd as well.
if you really just want a FILE * you should just be able to use fopen to open it, skipping the fd all together.
Consider your code:
char a[5];
a[4]='\0';
fread(a,2,1,fp);
This gives you a 5 character string. It is an "auto" variable and is not initialized. Then you terminate the string with a '\0'. Good. Then you fread one item of size 2. So bytes 3 and 4 are still garbage. Does this help?
Here are a few corrections to the program, as commented in the code.
#include <stdio.h>
#include <io.h> // added header
#include <fcntl.h>
int main()
{
FILE *fp;
int fd = open("y.txt",O_RDONLY);
printf("%d\n",fd); // add a newline for clarity
fp=fdopen(fd,"r");
//close(fd); // do not close before reading!
char a[5];
a[4]='\0';
fread(a,4,1,fp); // read 4 chars to fill the string space
printf("%s\n",a); // add a newline for clarity
close(fd); // close after reading
return 0;
}
File input:
abcde
Program output:
3
abcd
Although it would be better in the first place to have the simpler
FILE *fp = fopen("y.txt", "r");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
For example, I run my program like:
program.exe < text.txt
I want the program to read from file text.txt. How do I begin doing this?
Ok, as this is a textfile, you probably want to read it line by line, so
char buf[1024];
while (fgets(buf, 1024, stdin))
{
/* do whatever you need with the line that's in buf here */
}
Note your code doesn't know about the file, it just reads from standard input. With <, you tell your environment (CMD on windows, a shell like e.g. bash on *nix) to open that file for you and provide it to your program as the standard input instead of the default, the controlling terminal, which would normally just read from the keyboard.
Hint: 1024 is kind of a random pick, most text files don't have lines exceeding 1kb. You might want to modify it to better suit your expected input.
another way to do what you are looking for is
#include <stdio.h>
int main (void) {
int c;
while ((c = fgetc(stdin)) != EOF) fputc(c, stdout);
return 0;
}
some more help here
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).