Hey there, How do I go about copying text inside a text file into a multidimensional character array?
supposing the text file( text_file.txt) contained
this is the first line
this is the second line
this is the third line
#include <stdio.h>
int main(void){
FILE *f;
f=fopen("text_file.txt","r");
if (f==NULL){
printf("invalid!");
return 1;
}
else {
printf("successful");
}
char copied_text[80][80];
while (!feof(f)){
int i=0,j=0;
fgets(copied_text[i][j],"%s",f);
i++;
}
return 0;
}
-thank you.
I think your code almost work.
Just move the declaration of int i out of the loop.
Change the first parameter of fgets to copied_text[i] because it needs a pointer here.
Change the second parameter of fgets to 80 because it should be a int indicates the acceptable string length.
#include <stdio.h>
int main(void){
FILE *f;
f=fopen("text_file.txt","r");
if (f==NULL){
printf("invalid!\n");
return 1;
}
else {
printf("successful\n");
}
char copied_text[80][80];
int i=0;
while (!feof(f)){
fgets(copied_text[i],80,f);
++i;
}
for(int i = 0; i <3; ++i)
printf("%s\n", copied_text[i]);
return 0;
}
Related
I am working on a program that reads a txt file line by line and copies each line into an empty text file. First the program reads characters until the end of line, and stores all characters read in a buffer. After that it writes the buffer to a text file, and repeats this process until it reaches the end of file. The problem that I'm facing is: the program writes only the first line in the empty file. Can you help to solve this problems please, I'm a newbie in c.
this content txt file i want to read:
hey everyone
how are you
goood
and this the output of the programme:
hey everyone
this the Source code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int GetFileLenght(FILE*FP);
int main()
{
FILE *SourceFile;
FILE *DestinationFile;
int c;
int i=-1;
int x=1;
char buffer[350];
int lenght;
lenght=GetFileLenght(SourceFile);
printf("%d",lenght);
SourceFile=fopen("D:/SourceFile.txt","r");
while(i!=lenght)
{
c=fgetc(SourceFile);
printf("%c",c);
switch(c)
{
case '\n' :
DestinationFile=fopen("d:/des.txt","a+");
fputs(buffer,DestinationFile);
*buffer=0;
fclose(DestinationFile);
break;
default:
i++;
buffer[i]=c;
printf("%s",buffer);
}
}
return 0;
}
int GetFileLenght(FILE*FP)
{
FP = fopen("d:/SourceFile.txt", "a+");
fseek(FP, 0, SEEK_END);
int lengthOfFile = ftell(FP);
fclose(FP);
return lengthOfFile;
}
EDIT:
I have Made some changes to the program ,now it's good:
I'am also wondering if there is another way to add new line to file using fputs ?
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
FILE *SourceFile;
FILE *DestinationFile;
int c=0;
int i=-1;
int x=1;
char buffer[350];
SourceFile=fopen("D:/SourceFile.txt","r");
DestinationFile=fopen("d:/des.txt","a+");
while(x)
{
c=fgetc(SourceFile);
switch(c)
{
case '\n' :
fputs(buffer,DestinationFile);
fprintf(DestinationFile,"\n");
printf("%s",buffer);
strncpy(buffer, "", sizeof(buffer));
i=-1 ;
break;
case EOF :
fputs(buffer,DestinationFile);
x=0;
default:
i++;
buffer[i]=c;
}
}
fclose(DestinationFile);
return 0;
}
I am trying to write a program for some classwork that reads in a file using fscanf, and then stores each word into an array with one word in each element. Then I need to print each element of the array out on to a new line on the console.
The getty.txt file has the Gettysburg address in it with appropriate spacing, punctuation, and is multiline.
What I think is happening is that the entire text is being stored in the first element of the array, but I am not 100% sure as I am still learning to debug and write in C.
Any advice as to what I am doing wrong would be great! I currently only seem to be getting the last word and some extra characters.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void readFile();
void writeFile(char* buffer, int bufferlen);
FILE *fpread;
FILE *fpwrite;
char filebuffer[1000];
int filebufferlen = 0;
int main(int argc, char *argv[]) {
fpwrite = fopen("csis.txt", "w");
readFile();
writeFile(filebuffer, filebufferlen);
fclose(fpwrite);
return 0;
}
void readFile() {
char c;
filebufferlen = 0;
if(!(fpread = fopen("getty.txt", "r"))){
printf("File %s could not be opened. \n", "getty.txt");
fprintf(fpwrite,"File %s could not be opened. \n", "getty.txt");
exit(1);
}
while (!feof(fpread)) {
fscanf(fpread, "%s", filebuffer);
filebufferlen++;
}
}
void writeFile(char* filebuffer, int filebufferlen) {
for (int i = 0; i < filebufferlen; ++i){
printf("%c\n", filebuffer[i]);
}
}
After fixing the compile problems:
the code does not contain any code nor data declarations to contain an array of 'words.' So naturally, nothing but the last word is actually saved so it can be printed out.
I am trying to get the data from quiz_scores.txt to print to the screen so I can scan it into an array but I don't know how. Do I need to hard-code the file into my program and if so how?
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
int main(void)
{
//initializing variables
FILE *es;
FILE *hs;
int num=0;
int i;
int j;
int cols=10;
int rows=10;
int qid, s;
int quizarray[0][0];
int MAX_LENGTH=15;
char *result0;
char line[MAX_LENGTH];
FILE *qs="C:\\quiz_scores.txt";
qs = fopen("quiz_scores.txt", "r");
/*for (i=0; i<cols; i++){
for (j=0; j<rows; j++){
quizarray[i][j]=0;
fscanf(qs, "%d%d", quizarray[i][j]);
}
}
*/
while(fgets(line, MAX_LENGTH, qs))
{
printf("%s", line);
}
if(qs == NULL)
{
printf("Error: Could not open file\n");
return -1;
}
/*for (i=0;i<n;i++)
{
fprintf(qs, "%d\n");
}
fclose(qs);*/
return 0;
}
OK I think I know what you want to happen, read numbers from the file quiz_scores.txt into your 2d array and print them,correct?. I hope these corrections will allow you to do just that and also that you will understand why it wouldn't work before. The rest of the program is for you to do yourself, good luck!!
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
int main(void)
{
//initializing variables
int num=0,i,j,cols=10,rows=10;//just smooshed all onto the same line cause it was annoying me
int quizarray[10][10];//You declared a 2d array with 0 elements(you wanted one with ten rows and columns I presume 10 teams 10 rounds)
FILE *qs = fopen("quiz_scores.txt", "r");//Your declaration of the file pointer was incorrect
if(qs == NULL)//This if needs to go before you read anything from the file otherwise its kind of redundant
{
printf("Error: Could not open file\n");
return -1;
}
for (i=0; i<cols; i++)
{
for (j=0; j<rows; j++)
{
quizarray[i][j]=0;
fscanf(qs,"%d", &quizarray[i][j]);//Them scanfs need &s mate
\\and don't try to scan in two integer into one array element
printf("%d",quizarray[i][j]);
if(j<rows-1)
printf("-");
}
printf("\n");
}
fclose(qs);//Dont comment fclose out bad things will happen :P
return 0;
}
I need to write a code that searches all the aparition of a string in a file. Recently, my teacher told me to search string (char)255(char)255 in a file with the same string. The problem is that I can not read those characters and badly, I cannot distinguish or compare those caracter to EOF; My code for searching the given string in a file is :
//problema 14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char b;
int k=0;
if(argc!=3) {
fprintf(stderr,"Utilizare: %s fisier sir\n",argv[0]);
return 1;
}
if(argv[2][0]=='\0'){
fprintf(stderr, "String vid\n");
return 1;
}
FILE *f;
f=fopen(argv[1],"r");
if (!f)
{
perror(argv[1]);
return 1;
}
int i=0;
while((b = fgetc(f))!=EOF)
{
if(b==argv[2][i]) i++;
else {
fseek(f,-i, SEEK_CUR);
i=0;
}
if(argv[2][i]=='\0'){
k++;
fseek(f,-i+1, SEEK_CUR);
i=0;
}
}
printf("\nSULFUS %d APPEARANCES\n",k);
return EXIT_SUCCESS;
}
what can I do with this code to work on comparint string of (char)255 characters?
The trick is to realize that fgetc returns an int.
So, change the data type of bto int!
But then, "while" does nothing, because it seems that 255 is also recognized as EOF or it goes infinite. I tried also with int but I couldn't figure out much...This teacher is like a compilator, he likes to put you in trouble.
I wrote this code to read in the following line from a text file:
1019*/passed// 56.)100,/ 82//10 however when I print it afetr the sscanf function I only get the 1019 and the number 10 correctly , rest have garbage values , any idea why? I'd appreciate if someone could correct me, I even tried putting %d instead of just %[..] in the sscanf function but stil doesnt print out the desired values.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char buffer[100];
char status[20];
int grades[4]= {0};
int id;
int j;
int k;
int temp;
FILE *pointer = fopen ("testing5.txt", "r");
if((pointer = fopen("testing5.txt", "r"))!= NULL)
{
printf("No error, file successfully opened\n you may contine :)\n");
}
else
{printf("file could not be opened\nterminating program,...\n");
return 0;}
fgets(buffer,100,pointer);
printf("%s\n", buffer);
sscanf(buffer,"%[^*/]*/%[^//]//%[^.).)]%[^,/],/ %[^//]//%d",&id,status, &grades[0],&grades[1],&grades[2],&grades[3]);
printf("%d\n%s\n%d\n%d\n%d\n%d",id,status, grades[0],grades[1],grades[2],grades[3]);
for (j =0; j<3; j++)
for(k =j+1; k<4;k++)
{
if(grades[j]<grades[k] )
{
temp = grades[j];
grades[j] = grades[k];
grades[k] = temp;
}
}
printf("\nthe grades in ascending order is \n");
for (j =0; j<4; j++)
{
printf("%d\n", grades[j]);
}
return 0;
}
sscanf("1019*/passed// 56.)100,/ 82//10\n", "%d*/%[^/]// %d.)%d,/ %d//%d",
&id, status, &grades[0],&grades[1],&grades[2],&grades[3]);
Change
int grades[4]= {0}
to
char grades[4] = { 0 };
The warning output is pointing out the problem, don't ignore it. You are passing the wrong data types to sscanf.