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;
}
Related
i am trying to read from a text file and store it into an array character by character, ive tested it out by trying to print or check the ii count but it doesn't seem to be storing, any help would be muchly appreciated
char *readFile(char* filename)
{
FILE* f;
int ii = 0;
char* file = (char*)malloc(1000*sizeof(char));
char ch = '\0';
f = fopen(filename,"r");
if(f == NULL)
{
printf("Error opening file '%s'.\n", filename);
}
else
{
while ((ch = fgetc(f)) != EOF)
{
printf("%c",ch);
file[ii] = (char) ch;
ii++;
}
}
/* file[ii] = '\0'; setting last character as null*/
printf("\n");
fclose(f);
free(file);
return file;
}
I have commented out the line containing the code to free the character array before returning, which was basically making the pointer invalid. I have also changed the type of the variable "ch" to int as fgetc() returns integer.
#include <stdio.h>
#include <stdlib.h>
char *readFile(char* filename)
{
FILE* f;
int ii = 0;
char* file = (char*)malloc(1000*sizeof(char));
int ch; //changed to int from char.
f = fopen(filename,"r");
if(f == NULL)
{
printf("Error opening file '%s'.\n", filename);
}
else
{
while ((ch = fgetc(f)) != EOF)
{
// printf("%c",ch);
file[ii] = (char) ch;
ii++;
}
}
/* file[ii] = '\0'; setting last character as null*/
printf("\n");
fclose(f);
//free(file); //commented this line out
return file;
}
int main()
{
char *filename = "sample.txt";
char *file_arr = readFile(filename);
printf("%s \n",file_arr);
return 0;
}
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;
}
This function gives infinite loop. Any Help? And is it even possible to pass file stream to a function as argument.
#include<stdio.h>
#include<stdlib.h>
void fcopy(FILE *inFILE1){
FILE *inFILEcopy;
char a;
inFILEcopy=fopen("C:/Users/labuser.pcroot-PC.003/Desktop/empoleecopy.bak","w");
do{
a=fgetc(inFILE1);
fputc(a,inFILEcopy);
if(feof(inFILE1))break;
}while(1);
}
int main(){
FILE *inFILE;
inFILE=fopen("C:/Users/labuser.pcroot-PC.003/Desktop/empolee.dat","w");
fputs("My name is Anthony",inFILE);
fcopy(inFILE);
}
To summarize mine and Phil Brubaker comments, modify your code in this way:
#include<stdio.h>
#include<stdlib.h>
void fcopy(FILE *inFILE1) {
FILE *inFILEcopy;
char a;
inFILEcopy = fopen("C:/Users/scifani/Desktop/empoleecopy.bak", "w");
do{
a = fgetc(inFILE1);
fputc(a, inFILEcopy);
if (feof(inFILE1))break;
} while (1);
fclose(inFILEcopy);
}
int main(){
FILE *inFILE;
inFILE = fopen("C:/Users/scifani/Desktop/empolee.dat", "w");
fputs("My name is Anthony", inFILE);
fclose(inFILE);
inFILE = fopen("C:/Users/scifani/Desktop/empolee.dat", "r");
fcopy(inFILE);
}
FILE* fcopy(char* yourFile) { // Or parse a FILE like you did but with a pointer
FILE *inFILEcopy;
inFILEcopy = fopen("C:/Users/labuser.pcroot PC.003/Desktop/empoleecopy.bak", "w");
if (inFILEcopy == NULL)
return NULL; // You'll have to check null to see if fcopy() failed
FILE* inFILE1 = fopen(yourFile, "r");
if (inFILE1 == NULL) {
puts("File to be copied does not exist.\n");
return NULL;
}
for (char a = fgetc(inFILE1); feof(inFILE1);)
{
fputc(a, inFILEcopy);
if (ferror(inFILE1) || ferror(inFILEcopy)) { // If error in one of the two files
if (fclose(inFILE1))
puts("Couldn't close inFILE1\n");
if (fclose(inFILEcopy));
puts("Couldn't close inFILEcopy\n");
puts("Error during copy.\n");
return NULL;
}
}
return inFILEcopy;
}
int main() {
FILE *inFILE;
inFILE= fopen("C:/Users/labuser.pcroot-PC.003/Desktop/empolee.dat","w");
if (inFILE != NULL)
{
fputs("My name is Anthony", inFILE);
if (!ferror(inFILE) || fclose(inFILE)) // If no error when writing and closing works, we can copy
{
inFILE = fcopy("C:/Users/labuser.pcroot-PC.003/Desktop/empolee.dat");
if (inFILE != NULL)
puts("Copy success\n");
}
}
}
I think this is the best way to do this. I am open to any improvement though. Check this link if you have any question regarding the error checkings, someone explains the best way to do so. This should work perfectly.
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;
}
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;
}