Here is the code:
int main()
{
struct vinnaren
{
char vinnare[20];
int artal;
};
struct vinnaren v[10];
int inputrader;
int antalrader; //I want antalrader to be equal to the first
//line in test.txt(the first line is "5")
char file_name[256] = "test.txt";
char buf[512];
FILE *f = fopen(file_name, "r");
if (!f)
{
exit(0);
}
while (fgets(buf, sizeof buf, f))
{
printf("%s", buf);
}
fclose(f);
}
This is the code I have. I want to make it so that
antalrader = line1 in the file test.txt
How do I read a specific line from the file?
With this code you can read a file line by line and hence read a specific line from the text file:
lineNumber = x;
static const char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
int count = 0;
if ( file != NULL )
{
char line[256]; /* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{
if (count == lineNumber)
{
//use line or in a function return it
//in case of a return first close the file with "fclose(file);"
}
else
{
count++;
}
}
fclose(file);
}
else
{
//file doesn't exist
}
I got a really simple answer but I don't know if it is helping anyone:
int OpenCommand(int idOfCommand)
{
fscanf(file_ptr, "%[^idOfCommand]",a[idOfCommand]);
printf("%d\n", a[idOfCommand]);
system("pause");
return 0;
}
Related
I am trying to write a program in C within a file pointer, but the problem is I only managed to write the file path like this:
char *line = gtline("/home/ubuntu/games/cards-against-humanity/black-cards/single/1/file1.txt", 3);
but I would like to replace /home/ubuntu/games with something else so I can download the whole directory /cards-against-humanity and the program still points to the file /file1.txt.
I've tried with /~ and with /. but it gives me Segmentation fault I suppose when the program trys to open the file.
Thanks in advance.
Update: gtfile function is a function i created by myself here's the declaration:
char *gtline(char *s, int i)
{
FILE *file;
char *filename = s;
static char buffer[MAX_LINE];
int read_line = i;
file = fopen(filename, "r");
if (file == NULL)
{
printf("Error opening file.\n");
fclose(file);
return NULL;
}
bool keep_reading = true;
int current_line = 1;
do
{
fgets(buffer, MAX_LINE, file);
if (current_line == read_line)
{
keep_reading = false;
}
if (feof(file))
{
keep_reading = false;
}
else
{
buffer[strlen(buffer)-1] = '\0';
}
current_line++;
} while (keep_reading);
fclose(file);
return buffer;
}
I'm trying to read a text file in C. And I want to put the info in a struct.
I think I'm reading the file correctly but when I have to print the info, all the data is filled by the last line of the text.
Do you know why?
#include <stdio.h>
#define MAXCHAR 1000
int main() {
struct lumi {
char *domini;
char *disponible;
};
struct lumi registre[256];
int i = 0;
FILE *fp;
char str[MAXCHAR];
char* filename = "fitxer.txt";
fp = fopen(filename, "r");
if (fp == NULL){
printf("Could not open file %s",filename);
return 1;
}
while (fgets(str, MAXCHAR, fp) != NULL){
registre[i].domini = str;
registre[i].disponible = "offline";
i = i+1;
}
printf("%s", registre[0].domini);
printf("%s", registre[1].domini);
printf("%s", registre[2].domini);
printf("%s", registre[3].domini);
fclose(fp);
return 0;
}
The .txt I tried is
Hi
My
Name
Is
I am very new to working with files and I can't seem to get my head around this. What I am trying to do is to write in the Exit.txt file all the lines that have my given word in them. For example, if my word is "exercise" and my In.txt contains the following:
I exercise daily
I like apples
How often do you exercise
I am tired
Then in Exit.txt I should have
I exercise daily
How often do you exercise
The problem is that somehow it only writes the last line in the Exit.txt file, and sometimes it doesn't even write anything, depending on my input In.txt.
I would very much appreciate any help, thank you very much!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE* fis, * fis2;
char* sir, * rez, word[50];
printf("Word: ");
gets(word);
sir = malloc(50 * sizeof(char));
fis = fopen("In.txt", "rt");
if (fis == NULL)
printf("Can't open file!");
else
{
while (!feof(fis))
{
rez = fgets(sir, 50, fis);
if (strcmp(rez,word)==0)
{
fis2 = fopen("Exit.txt", "wt");
fputs(sir, fis2);
}
}
}
fclose(fis);
free(sir);
return 0;
}
When you open a file for writing in a loop you must know about the offset. Its better open file in append mode, write your data and close it.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE* fis, * fis2;
char* sir, * rez, word[50];
char *line = NULL;
ssize_t bufsiz = 0;
ssize_t nbytes;
printf("Word: ");
gets(word);
fis = fopen("In.txt", "rt");
if (fis == NULL)
printf("Can't open file!");
else
{
while ((nbytes = getline(&line, &bufsiz, fis)) != -1)
{
char * ptr_value = strstr(line,word);
if(ptr_value != NULL) {
printf(line);
fis2 = fopen("Exit.txt", "a");
fputs(line, fis2);
fclose(fis2);
}
}
}
fclose(fis);
return 0;
}
there are a few changes needed to get your code working:
only open the output file once (so not in the while loop)
to check if a string is a part of another string please use "strstr" and not "strcmp"
don't forget to close your file at the end
So here is a suggestion for a solution
int main()
{
FILE* fis, * fis2;
char* sir, * rez, word[50];
printf("Word: ");
gets(word);
sir = malloc(50 * sizeof(char));
fis = fopen("In.txt", "rt");
if (fis == NULL)
printf("Can't open file!");
if ((fis2 = fopen("Exit.txt","wt"))==NULL){
printf("Cant't open Exit-file\n");
return EXIT_FAILURE;
}
else
{
while (!feof(fis))
{
rez = fgets(sir, 50, fis);
if(strstr(rez,word)!=NULL)
{
fputs(sir, fis2);
}
}
}
fclose(fis);
fclose(fis2);
free(sir);
return 0;
}
I have a text file with one number in each line.
I want to read a specific number of lines from this file , lets say the first 20, what is the way to do it?
I have the following piece of code.
FILE *ft;
ft=fopen(filename,"r");
for(int k=1; k<Nt+1; k=k+1)
{
fscanf(ft,"%lf\n",&curve3[k]);
printf("%lf\n",curve2[k]);
}
EDIT: I changed my code to
FILE *ft;
ft=fopen(filename,"r");
int k=1;
while(!feof(ft))
{
if(k<=Nt)
{
fscanf(ft,"%lf\n",&curve2[k]);
//printf("%lf\n",curve2[k]);
}
k=k+1;
}
It still doesn't seem to work.
With this code you can read a file line by line and hence read a specific line from the text file. So, you can modify the code.
lineNumber = x;
static const char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
int count = 0;
if ( file != NULL )
{
char line[256]; /* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{
if (count == lineNumber)
{
//use line or in a function return it
//in case of a return first close the file with "fclose(file);"
}
else
{
count++;
}
}
fclose(file);
}
else
{
//file doesn't exist
}
I am trying to print a certain line of a file in c. So far I think I am successfully reading line 8 of my text file but my question is how do I print that line using this code?
Thanks!!
this is the code so far:
int lineNumber = 8;
static const char filename[] = "Text.txt";
FILE *file = fopen(filename, "r");
int count = 0;
if ( file != NULL )
{
char line[256]; /* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{
if (count == lineNumber)
{
//use line or in a function return it
//in case of a return first close the file with "fclose(file);"
}
else
{
count++;
}
}
fclose(file);
}
This works perfectly fine.
Are you missing the main function OR is it just the code snippet you have posted ?
int lineNumber = 8;
static const char filename[] = "Text.txt";
int main()
{
FILE *file = fopen(filename, "r");
int count = 0;
if ( file != NULL )
{
char line[256]; /* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{
if (count == lineNumber)
{
//use line or in a function return it
// //in case of a return first close the file with "fclose(file);"
printf("\n str %s ", line);
fclose(file);
return 0;
}
else
{
count++;
}
}
fclose(file);
}
return 0;
}