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 last month.
Improve this question
I m new to programing and I wonder if anyone could explain to me how to memorate a line from a file in to a variable of a struct? And then print it?:")
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
typedef struct{
char name[100];
}p;
void function(FILE* pfile, p me)
{
rewind(pfile);
char buffer[100];
fgets(buffer, 100, pfile);
strcpy(me.name, buffer);
printf("%s\n", me.name);
}
int main()
{
FILE* pfile = fopen("text.txt", "r");
p me;
function(pfile, me);
fclose(pfile);
return 0;
}
/////This is the code in c. The file contains only a name(your choise);
function(pfile, me); passes a copy of me to function();. Surely you want function() to affect's main's me. Pass the address of me and change function signature.
void function(FILE* pfile, p *me) {
rewind(pfile);
if (fgets(me->name, sizeof me->name, pfile) == NULL) {
me->name[0] = '\0';
}
}
int main(void) {
FILE* pfile = fopen("text.txt", "r");
if (pFile) {
p me;
function(pfile, &me);
fclose(pfile);
printf("<%s>\n", me.name);
}
return 0;
}
Related
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 2 years ago.
Improve this question
I am confused that how to give break line after every word which is present in a file.
Words in text file
Name Date of birth <---- I put this in code
John 02\02\1999 <---- I want to jump to this line
I want this
Here is your: Name
Here is your: Date of Birth
But it is giving me this
Here is your: N
Here is your: a
Here is your: m
Here is your: e
And I don't know how to get it.
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fr = fopen("/home/bilal/Documents/file.txt","r");
char ch;
if(fr != NULL){
while(!feof(fr)){
ch = fgetc(fr);
printf("Here is your %c\n: ", ch);
}
fclose(fr);
}
else{
printf("Unable to read file.");
}
return 0;
}
Within your while loop instead of immediately printing the character that you read, store the char in a char array. Add an if statement that does a comparison that checks if the read char is a space character. If it is you should print the stored array and set the index of the array back to 0.
Example:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fr = fopen("file.txt","r");
char ch[100];
int index = 0;
if(fr != NULL){
while((ch[index] = fgetc(fr)) != EOF){
//printf("%c\n", ch[index]);
if(ch[index] == ' ') {
ch[index] = '\0';
printf("Here is your: %s\n", ch);
index = 0;
}
else {
index++;
}
}
fclose(fr);
}
else{
printf("Unable to read file.");
}
return 0;
}
Based on the line of text of the file provided we can assume that if the first letter of the word is in uppercase then it is the start of the next sentence:
Name Date of birth ID card number Phone number Address Account Fixing year
And use this to divide the line into sentences.
So here is the code by Christopher changed to group the words into sentences:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
FILE * fr = fopen("file.txt","r");
char ch[100];
int index = 0;
if(fr != NULL){
while((ch[index] = fgetc(fr)) != EOF){
if(index > 0 && ch[index-1] == ' ' && isupper(ch[index])) {
ch[index-1] = '\0';
printf("Here is your: %s\n", ch);
ch[0] = ch[index];
index = 1;
}
else {
index++;
}
}
ch[index] = '\0';
printf("Here is your: %s\n", ch);
fclose(fr);
}
else{
printf("Unable to read file.");
}
return 0;
}
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 4 years ago.
Improve this question
I am working on a project. there is a .txt file that includes negative integers like this
0 -1 0
-1 20 -1
0 -1 0
My problem is I couldn't read negative numbers. What is your suggestions for this problem?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char * argv[]) {
FILE *fptr;
char letter;
fptr = fopen(argv[1],"r");
if(fptr == NULL){
printf("Please provide an argument\n");
exit(1);
}
while ( ( letter = fgetc(fptr) ) != EOF ) {
printf("%c",letter);
}
printf("\n");
fclose(fptr);
return 0;
}
You can try this to read the lines from the file and then parse your numbers.
getline reads a line from your file and strtok then gives you a string until the specified delimeter (space in your case). Hope this helps.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char * argv[]) {
FILE * fptr = fopen(argv[1],"r");;
char * line = NULL;
size_t lineSize = 0;
if(fptr == NULL){
printf("Please provide an argument\n");
exit(1);
}
while(getline(&line, &lineSize, fptr) != -1) {
line[strcspn(line, "\n")] = 0;
printf("%s\n", line);
char * token = strtok(line, " ");
printf("%s\n", token);
while((token = strtok(NULL, " ")) != NULL) {
printf("%s\n", token);
}
}
fclose(fptr);
return 0;
}
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 6 years ago.
Improve this question
How do I convert this program(binary.c) into a command line application so it can read any binary text (e.g. binary1.txt) file and then convert it to ASCII? For example I want to do:
./binary < binary1.txt
and it should print
Hello World!
at the command line.
This is my program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f_ptr;
f_ptr = fopen("binary1.txt", "r");
if (f_ptr == NULL){
printf("Error opening binary1.txt");
return 1;
}
while(1){
int ch = fgetc(f_ptr);
if(ch == EOF)
break;
printf("%c",ch);
}
fclose(f_ptr);
return 0;
}
I am not sure what you are asking. I guess you need to compile your source using a compiler. If you are using Linux you could do:gcc binary.c -o binary to get an executable file...
#include <stdio.h>
int main(int argc, char *argv[]){
FILE *fp;
if(argc < 2)//./binary < binary1.txt
fp = stdin;
else {//./binary binary1.txt
if((fp=fopen(argv[1], "r")) == NULL){
perror("fopen");
return 1;
}
}
int ch, count = 0;
unsigned char out = 0;
while((ch = fgetc(fp))!=EOF){
if(ch == '1' || ch == '0'){
out <<= 1;
out += ch - '0';
if(++count == 7){//7bit code
printf("%c", out);
count = out = 0;
}
}
}
puts("");
fclose(fp);
return 0;
}
From what I understand you are trying to pass the filename to your program and not sure how to do that. In that case you might want to do this
int main(int argc, char* argv[])
{
if(argc>1)
std::string filename = argv[1];
//now you have the file in the filename
// your code
return 0;
}
so when you execute just do this
./binary binary1.txt
you don't need to use < and I am assuming that both your executable and text file are in the same directory
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 do get number line of file text on c ?. Help me .
Get sum number line.
I want read a file text.
EX:
for( line = 0; line < sumline; line ++) {
printf("char in line");
}
In case i understood the question :
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
char * line;
size_t len = 0;
ssize_t read;
int lines = 0;
fp = fopen("input.txt", "r");
if( fp != NULL ){
while ((read = getline(&line, &len, fp)) != -1){
lines ++;
printf("%s\n", line);
}
fclose(fp);
}
printf("number of lines : %d\n", lines);
}
to count how many lines in your file
Try this:
`int lines = 0;
while ((read = getline(&line, &len, fp)) != -1) {
lines++;
}
cout << lines << endl;`
You can use the following function to get the number of lines inside a file.
#include <stdio.h>
// get the number of lines inside file
int getLineCnt(char *pcFileName) {
FILE *fp;
int lines=0;
fp = fopen(pcFileName, "r");
if(fp == NULL) { return -1; }
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp, "%*c"))) {
++lines;
}
io_fclose(fp);
return lines; ///\ retval number of lines
}
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 8 years ago.
Improve this question
Compilable Code
`
#include <stdio.h>
#include <stdlib.h>
FILE * openFile(int argc, char *argv[]);
int readMonth(FILE *fin);
int main(int argc, char *argv[])
{
int month, choice;
int * temps;
FILE * fin = NULL;
fin = openFile(argc, argv);
month = readMonth(fin);
}
FILE * openFile(int argc, char *argv[])
{
int test;
FILE * fin = NULL;
fin = fopen(argv[2], "r");
fscanf(fin, "%d", &test);
if(fin==NULL){
perror("fopen");
exit(1);}
return fin;
}
int readMonth(FILE *fin)
{
int month=0;
int n = fscanf(fin, "%d", &month);
if(n!=1)
{printf("error reading month from file\n");
exit(1);}
printf("%d\n", month);
return month;
}`
I am trying to read data out of a text file and I'm getting weird results.
I call fileOpen from main to return the file pointer, this is my function:
FILE * fileOpen(int argc, char *argv[])
{
FILE* fin = NULL;
fin = fopen(*argv, argc);
return fin;
}
then calling readMonth to read the first line of the file, it does not print the correct
int.
int readMonth(FILE *fin)
{
int month=0;
fscanf(fin, "%d", &month);
printf("%d", month);
return month;
}
I am not sure if the error is with the file opening or the reading the file.
Here are some suggestions. 1. Check the return value of fopen
FILE *fileOpen(int argc, char *argv[] {
FILE* fin = NULL;
fin = fopen(*argv, "r");
if(fin == NULL) { // this block is new
perror("fopen");
exit(1);
}
return fin;
}
Note: If the argc and argv are from main, then *argv is argv[0] which is the name of the program. You probably want argv[1]. And you should check that argc >= 2 in main.
2: Check the return value of fscanf:
int readMonth(FILE *fin)
{
int month=0;
int n = fscanf(fin, "%d", &month);
if(n != 1) { // this block is new
printf("error reading month from file\n");
exit(1);
}
printf("%d", month);
return month;
}
With these two changes your code should function properly.
You can read the first line of the file using fgets in function readmonth
char buffer[1024];
char *l ;
if((l=fgets(buffer,sizeof(buffer),fin))!=NULL)
printf("%d",atoi(l));
Also , you can check if the file opening failed using
if(fin == NULL)
printf("failed");
FILE * file = fopen(data, "r");
if(file==NULL){
printf("404 File Not Found\n");
exit(-1);
}
while(1)
{
char line[100];
int res = fscanf(file, "%s", line);
if(res == EOF)
break;
fscanf(file, "%s", &var);
// handle the var
}