returning permutes in a string in C - c

I need to write a function that returns every permute of a word in a given text file.
For some reason the output is wrong and I don't really understand why.
However, if instead of writing a function that should check a presnce of a letter (as seen below the function chars(char,char*))
I write the needed letters for check manually
it works as intended.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
void permute(FILE *fp, char *perm){
int c,i,perm_size=0;
int flag,chars(char,char *);
char permute[MAX];
i=0;
while(perm[i++])
perm_size++;
flag=0;
i=0;
while(!feof(fp)){
c=fgetc(fp);
if(chars(c,perm)==0){/*a function that checks c with each one of the permutes chars*/
flag++;
permute[i++]=c;
if (flag == perm_size){/*if the permute is the word's length*/
permute[i]='\0';
printf("%s\n",permute);
flag=0;
i=0;
permute[0]='\0';
}
}
else{
flag=0;
i=0;
}
}
}
int chars(char ch, char *str){
while(*str++)
{
if(ch==*str)
return 0;
}
return 1;
}
#include "func.h"
#include <string.h>
int main(int argc,char **argv){
FILE *fp;
char *input,*perm;
char *prog=argv[0];
void permute(FILE *, char *);
if(argc==1)
{
fprintf(stderr,"%s error: no arguments\n",prog);
exit(1);
}
input=argv[1];
perm=argv[2];
if(!(fp=fopen(input,"r")))
{
fprintf(stderr,"%s error: cannot open file\n",prog);
exit(1);
}
permute(fp,perm);
fclose(fp);
return 0;
}
input:
./program text chairs
output:
nothing

As told - you should really learn debuging your programs, so I did - I incremented wrongly a value in a function chars(char , char *)
a good pointer explanation from other user

Related

How can I read a text file and convert that into a 2D array of characters?

I've tried using the method indicated here but I'm unable to pinpoint each character from the array "buffer". My end goal is to make one array for each sentence with one word in each row so I need to first know where each character is.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define LENGTH 100
bool input(char *file_name, char *phrase)
{
FILE *file;
bool file_valid;
int i, j, k, l;
file = fopen(file_name, "r");
if (file != NULL)
{
file_valid = true;
while(fgets(phrase, LENGTH, file))
{
printf("%s", phrase);
}
printf("\nThe file was loaded succesfully.\n");
fclose(file);
}
else
{
file_valid = false;
printf("\nWe couldn't find a file with the specified name. Closing program.");
}
}
int main()
{
char file_name[20], phrase[LENGTH];
int i, j;
printf("Please introduce the name of the file: ");
scanf("%s", file_name);
input(file_name, phrase);
return 0;
}
There are some variables and headers that aren't necessary atm but I will be using them later on.

C program : print in text file 2 numbers in row

I need c program that save numbers it txt file, but numbers must be 2 in row.
For now I have this code, but how to print numbers 2 in row.
I really hope that someone can help me for that problem.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <ctype.h>
#define max 80
#define n 30
#define space ' '
void inputText(char text[][max], int *len);
void writeText(FILE *fp, char *text);
int main(){
char text[n][max];
int i=0, len=0;
FILE *fp=NULL;
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
inputText (text,&len);
fp=fopen("test.txt", "a");
if(fp!=NULL)
for (i=0;i<len;i++)
writeText (fp, text[i]);
else
printf("Error!");
fclose(fp);
return 0;
}
void inputText(char text[][max], int *len)
{ char ch;
int i=0, s=0;
printf("Input text:");
while ((gets(text[i]))!=NULL)
{ i++;
(*len)++;
}
}
void writeText(FILE *fp, char *text)
{ fputs(text,fp);
fprintf(fp,"\n");
}
Perhaps change the function writeText to something like this
void writeText(FILE *fp, char *text, int current)
{ fputs(text,fp);
if(current % 2 == 0)
{
fprintf(fp,"\n");
}
}
The fprintf(fp,"\n"); statement (inside writeText()) is what makes the line breaks in your output. You should figure out a way to run that statement only after the 2nd, 4th, 6th, ... number output.

Read from file and assign as string and call function as argument

I try to use passing function as argument but I'm stuck. I have two questions:
First, I try to call uppercase and open .txt in tfm Second, How can I read characters from in.txt as string and assign to char content[] ?
#include <stdio.h>
void tfm( char str_filename[], void(*pf_convertion)( char content[]));
void uppercase(char content[]); //converts all letters to uppercase
int main(){
puts("-------------------------------");
printf("tfm:\n");
tfm("in.txt", uppercase);
puts("-------------------------------");
return 0;
}
void tfm( char str_filename[], void(*pf_convertion)( char content[])){
FILE *fptr_in;
if((fptr_in=fopen(content,"r"))==NULL){
printf("Error reading file\n");
}
else{
(*pf_convertion)(str_filename);
}
}
void uppercase(char content[]){
char ch;
int st;
for(st=fscanf(fptr_in,"%c",&ch);
st==1;
st=fscanf(fptr_in,"%c",&ch)){
if('a'<=ch && ch<='z'){
ch-=32;
printf("%c",ch);
}
else
printf("%c",ch);
}
}

Is it possible to read a text file without using arrays? C

#include <stdio.h>
#include <stdlib.h>
int main (int argc, const char * argv[])
{
FILE *f;
char *chPtr = malloc(sizeof(char)*1000);
/////////////////
f=fopen("input.txt", "r");
if(!f)
return 1;
while (fgets(*chPtr,1000,f)!=NULL) {
printf("%c", *chPtr);
}
fclose(f);
printf("\n%c", *(chPtr+4));
return 0;
}
Our teacher said don't use [] 'brackets' so I'm assuming it's up to malloc, but couldn't make it work?
replace this fgets(*chPtr,1000,f) with this fgets(chPtr,1000,f) and (as #MattMcnabb mentionned)printf("%c", *chPtr) with printf("%s", chPtr)

Read From File and Store Strings into a Multidimensional Array of Strings in C

I really need to know how to fix this.
I have a file that is read and I store the strings from the file into an array that is passed as an argument, but I can't figure out how to make it work.
When I do print the content of the array it says null.
So how do I pass a multi-dimensional array of strings to readfiles() and make it save the strings in the array passed as parameter, each string in one position?
Thanks for the help.
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <strings.h>
#define max_chars_string 10000
#define n_strings 100
int main(int argc, char **argv)
{
char *filename;
char strings_hashes[n_strings][max_chars_string];
char * pointer_string = &strings_hashes[0][0];
int n_lines;
int i = 0;
filename = (char*)malloc(strlen(argv[1])*sizeof(char));
if(argc !=3){
fprintf(stderr, "Usage : %s [text_file] [cores]",argv[0]);
exit(0);
}
strcpy(filename,argv[1]);
read_lines(filename,pointer_string);
for(i = 0; i<n_lines;i++){
printf("%s \n",strings_hashes[i][max_chars_string]);
}
return 0;
}
void read_lines(char * filename, char *pointer){
FILE *fp;
char str[max_chars_string];
int i =0;
if((fp = fopen(filename, "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) {
while(fgets(str, sizeof str, fp)) {
strcpy(pointer, str);
printf("%s", str);
pointer++;
i++;
}
}
fclose(fp);
}
Change
void read_lines(char * filename, char *pointer){
to
void read_lines(char * filename, char (*pointer)[max_chars_string]){
(pointer's type needs to be "pointer to array of max_chars_string chars". When using pointers to access multidimensional arrays, the pointer type needs to know the all the dimensions except for the outermost one, so that it knows how far to skip along when incremented.)
Change
strcpy(pointer, str);
to
strcpy(*pointer, str);
Now call it as
read_lines(filename,strings_hashes);
(This is equivalent to the following, which may be clearer:)
read_lines(filename,&string_hashes[0]);
Finally, you want to print a string not an individual character, so use
printf("%s \n",strings_hashes[i]);

Resources