I want to read the words from a text file into an array.
Why does this code work with a 2D array (a[50][50]) but not with a 1D array (a[50])?
This code prints what I want but it also print some other useless characters. What causes this?
void inputwords(){
int i=0;
char wrd[50];
FILE * fptr;
char fname[20]="txt.file";
fptr=fopen(fname,"w");
if(fptr==NULL) {
printf("error in opening file!");
exit(1);
}
while(wrd!='\0'){
fgets(wrd,sizeof wrd,stdin);
fprintf(fptr,"%s",wrd);
if(wrd[i]=='*' && wrd[i+1]=='*' && wrd[i+2]=='*' && wrd[i+3]=='*' &&
wrd[i+4]=='T' && wrd[i+5]=='E' && wrd[i+6]=='L' && wrd[i+7]=='O' &&
wrd[i+8]=='S') {
break;
}
}
fclose(fptr);
return;
}
void readfile(){
FILE *fptr;
char a[50][50];
int i=0;
char fname[20]="txt.file";
fptr=fopen(fname,"r");
while(fgets(a[i],50,fptr)){
i++;
}
for(i=0;i<50;i++){
printf("%s",a[i]);
}
fclose(fptr);
return;
};
main(){
inputwords();
readfile();
return(0);
}
for(i=0;i<50;i++){
printf("%s",a[i]);
}
This prints out the value of every pointer in a[50]. Once you reach past the char pointers which are actually set to point to something, you're just printing out the value of the pointer itself.
for(int n=0; n<i; n++){
printf("%s",a[n]);
}
Would work.
I just tested the answer of Cowbolt, it works , and also wanted to ask you why do you have the line in inputwords() :
while(wrd!='\0') {
The condition of null character will never happen from user input. It is better to have clear instructions and say :
printf("Enter text (to finish input, type on a line of its own:****TELOS ):");
while (1) {
/*be aware that your final i marks the position of string "****TELOS" */
/* so if you dont want it to output, have the n<i-1 in Cowbolt solution*/
Related
I have written the following code in c to read words in a text file, but the code is not working, please correct it.
I have a file a.txt,in it:
Coding
So I want the word 'Coding' to be stored into array b.
q=fopen("a.txt","r");
d=fgetc(q);//q is pointer to text file
while(d!=EOF)
{
i=0;
while((d!='\n')&&(d!=EOF));
{
b[i++]=d;
d=fgetc(q);
}
b[i]='\0';
if(d==EOF)
break;
d=fgetc(q);
}
If you're not mallocating memory, then below would be my approach
int c;
char myword[20]; // max characters to store is 20
int i=0;
FILE* ptr=fopen("38518211","r");
if (ptr==NULL){
printf("Can't open the file");
}
else{
while(i<19 && (c=fgetc(ptr)) != EOF)
myword[i++]=c;
}
if((c=fgetc(ptr)) != EOF)
printf("Original string is truncated to fit into alloted space\n");
myword[i]='\0'; // Null terminating the string
printf("String from file %s\n",myword);
fclose(ptr);
I am searching a string inside the the content of a file and storing the whole content in a buff char[] by excluding the space ' ' and at last comparing the this buff char[] with user input string for checking the availability.
But I am unable to store the whole file content because fgetc() is checking the space in if condition and placing to the next char even though I tried to use fseek() for pointing to the 1 char backward from the current position; it is making my program to terminate.
Please help me; my code follows:
#include <stdio.h>
#include <stdlib.h>
FILE *file;
int file_size;
int main(void) {
file= fopen("C:/Users/home/Desktop/dummy/s.txt","r");
file_exist(file);
fclose(file);
return 0;
}
void file_exist(file)
{
if(file)
{
printf("file exists\n");
content_exist(file);
}
else
{
printf("it doesnt exist\n");
}
}
void content_exist(file)
{
fseek(file,0,SEEK_END);
file_size=ftell(file);
rewind(file);
char user_input[10];
if(file_size==0)
{
printf("content does not exists\n");
}
else
{
printf("content exist\n");
printf("enter the word needs to be matched\n");
scanf("%s",&user_input);
check_string(user_input);
}
}
void check_string(user_input)
{
char buff[file_size];
int temp=0;
while(!feof(file))
{
printf("hi\n");
if(fgetc(file)!=' ')
{
fseek(file, -1,SEEK_CUR);
buff[temp]= fgetc(file);
temp++;
}
}
if(strcmp(user_input,buff)==0)
{
printf("your content matched\n");
}
else
{
printf("your content not matched\n");
}
}
For your purpose, there doesn't seem to be any reason to use fseek.
Change this:
if (fgetc(file) != ' ')
{
fseek(file,-1,SEEK_CUR);
buff[temp] = fgetc(file);
temp++;
}
To this:
buff[temp] = fgetc(file);
if (buff[temp] != ' ')
temp++;
And of course, in order to use strcmp safely, you must terminate buff with a null-character:
buff[temp] = 0;
if (strcmp(user_input,buff) == 0)
...
Hence, please note that for a file with no space characters you will need char buff[file_size+1].
Fseek with integer values like -1 only work on binary files. Source
Try fopen with "rb" instead of just "r"
I've been trying to sort these names from the Euler #22 problem. I tried many ways to swap strings. I had problem every time. In some, there were random symbols; in others i had overflowed(?) names while swapping(Like ending up with LINDACIA while trying to swap PATRICIA and LINDA). I tried to add a symbol each free byte in string (like LINDAzzzzzzzzz\0) and got compiler error.
Input File
"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET"
Source Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *nameorder;
char liste[5164][15];
char hold[15];
int i=0,j=0,k;
int c;
FILE *ptr;
nameorder ="names.txt";
ptr= fopen( nameorder , "r");
if(ptr==0)
{
printf("File could not be opened");
return 0;
}
while( (c=getc(ptr)) != EOF) //reading character by character
{
if(c=='"')
{
continue;
}
if(c==',')
{
j=0;
i++;
continue;
}
liste[i][j]=c;
j++;
}
fclose(ptr);
for(k=0;k<5163;k++)
{
for(i=0;i<5164;i++) //
{
if( strcmp(liste[i],liste[i+1])>0 )
{
strncpy(hold,liste[i],15);
strncpy(liste[i],liste[i+1],15);
strncpy(liste[i+1],hold,15);
}
else
continue;
}
}
At the very least, you don't null terminate your strings. This will matter for all of the names that are not exactly 15 characters long.
The easiest way to fix this is to initialize your entire array to zero.
char liste[5164][15] = {{0}};
The code is supposed to read a user-inputted text file name, copy every character into a multidimensional array, then display it with standard output. It compiles, but produces unintelligible text. Am I missing something?
for (i = 0; i < BIGGEST; i++) {
for (j = 0; j < BIGGESTL; j++) {
if (fgetc(array, fp) ) != EOF)
array[i][j] = c;
else array[i][j] = '\0'
}
fclose(fp);
return 0;
}
You stop filling the array when you encounter EOF, but you print the full array out no matter what.
If the data read from the file is smaller than the input array, you will read that data in and then print that data out, plus whatever random characters were in the memory locations that you do not overwrite with data from the file.
Since the requirement seems to be to print text data, you could insert a special marker in the array (e.g. '\0') to indicate the position where you encountered EOF, and stop displaying data when you reach that marker.
You had better read each line from file
For example:
int i = 0;
while(fgets(text[i],1000,fp))
{
i++;
}
Though the question is edited and only part of the code is left in question. I am posting more than what is required for the question at the moment.
Reason being, there can be numberous improvements to originally posted full code.
In main() function:
You need to check for the argc value to be equal to 2 for your purpose and only then read in value of argv[1] . Else if program executed without the command-line-argument which is file_name in this case, invalid memory read occurs, resulting in segmentation fault if you read in argv[1].
In read_file_and_show_the contents() function:
Stop reading file if end of file is reached or maximum characters is read and store in the character array.
Below Program will help you visualize:
#include <stdio.h>
/*Max number of characters to be read/write from file*/
#define MAX_CHAR_FOR_FILE_OPERATION 1000000
int read_and_show_the_file(char *filename)
{
FILE *fp;
char text[MAX_CHAR_FOR_FILE_OPERATION];
int i;
fp = fopen(filename, "r");
if(fp == NULL)
{
printf("File Pointer is invalid\n");
return -1;
}
//Ensure array write starts from beginning
i = 0;
//Read over file contents until either EOF is reached or maximum characters is read and store in character array
while( (fgets(&text[i++],sizeof(char)+1,fp) != NULL) && (i<MAX_CHAR_FOR_FILE_OPERATION) ) ;
//Ensure array read starts from beginning
i = 0;
while((text[i] != '\0') && (i<MAX_CHAR_FOR_FILE_OPERATION) )
{
printf("%c",text[i++]);
}
fclose(fp);
return 0;
}
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Execute the program along with file name to be read and printed. \n\
\rFormat : \"%s <file-name>\"\n",argv[0]);
return -1;
}
char *filename = argv[1];
if( (read_and_show_the_file(filename)) == 0)
{
printf("File Read and Print to stdout is successful\n");
}
return 0;
}
I was wondering how to properly read a file and place each line in a string of arrays in C.
I have a file with the following written on it
one
two
three
four
I tried writing something like this:
int read_file(FILE *fp){
char readLine[MAX_LEN];
char *myarray[20];
int counter =0;
int i =0;
while(fgets(readLine,MAX_LEN,fp) != NULL){
myarray[counter] = readLine;
counter++;
}
/*printing the array*/
while(i<counter){
printf("%d %s",i,myarray[i]);
i++;
}
}
and the main would be something like
int main(){
FILE *fp;
fp = fopen("my.txt","r");
if(fp == NULL){
fprintf(stderr,"File does not exist");
return EXIT_FAILURE;
}
read_file(fp);
}
however, when printing I get:
four
four
four
four
even when I print using printf("%s",myarr[2]) , I still get four
Anyone knows what the problem may be?
You really need to make a copy of the line (by way of strdup()) as you are overwriting the buffer used to accept the input:
int read_file(FILE *fp){
char readLine[MAX_LEN];
char *myarray[20]; // Note char pointer!
int i, counter = 0;
while (counter < 20 && fgets(readLine,MAX_LEN,fp) != NULL) { // Note limit!
myarray[counter] = strdup(readLine);
counter++;
}
/*printing the array*/
for (i = 0; i < counter; i++)
printf("%d %s",i,myarray[i]);
/* free the lines */
for (i = 0; i < counter; i++)
free(myarray[i]);
}
myarray[counter] = readLine;
is the problem. You are overriding the read line pointer values each time.
use strcpy to copy the buffer content instead.
In addition as commented: you are not declaring array of strings, merely one string.
Change it to:
char myarray[4][20];
Of course, 4 is an example. Change it to any number of lines or use dynamic allocation.