trying to read a sentence from user and write in text file - c

I'm trying to create something where I need to receive a sentence from the user and it is then written in a text file. I know how to do this with integers and float but it doesn't seem to work with char.
main()
{
unsigned char word;
FILE *f;
errno_t err;
printf("Enter Text: ");
scanf_s("%c", &word);
err = fopen_s(&f, "testwrite.txt", "w");
fprintf_s(f, "%c", word);
fclose(f);
return 0;
}

to get input from the user:
char input[1000];
scanf("%s", input);
and to write it to a file:
fputc(input, f);
and the final piece code should look like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char input[1000];
FILE *f;
f = fopen(fileName ,"w+");
printf("enter text:");
scanf("%s", input);
fputc(input, f);
fclose(f);
return 0;
}

Related

C program to find words containing letter in file

I want to write a program to print all words containing a letter ("D" for example) in a text file.
This is what I came up with, but it does not work.
I get the core dumped error.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1000
int findWord(FILE *file , const char *letter);
void main()
{
FILE *file;
char path[100];
char letter[1];
printf("Enter file path: ");
scanf("%s", path);
printf("Enter letter to search in file: ");
scanf("%s", letter);
file = fopen(path, 'r');
if (file == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write priveleges.\n");
exit(EXIT_FAILURE);
}
findWord(file, letter);
fclose(file);
return 0;
}
int findWord(FILE *file, const char *letter)
{
char str[BUFFER_SIZE];
while ((fgets(str, BUFFER_SIZE, file)) != NULL)
{
if (str == letter)
{
printf(letter);
}
}
}
Edited your code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
int findWord(FILE *file , const char letter);
int main()
{
FILE *file;
char path[100];
char letter;
// const char *fpath = "/home/kozmotronik/Belgeler/karalama.txt";
printf("Enter file path: ");
// fgets(path, sizeof(path), stdin);
// printf("Entered file path: %s\n", path);
scanf("%s", path);
// flushes the standard input
// (clears the input buffer)
// If we don't do this we cannot get the letter from the input buffer
while ((getchar()) != '\n');
printf("Enter letter to search in file: ");
letter = getchar();
// Validity check
if(letter < '0' || letter > 'z') {
printf("Entered character is not valid\n");
exit(EXIT_FAILURE);
}
file = fopen(path, "r");
if (file == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write priveleges.\n");
exit(EXIT_FAILURE);
}
findWord(file, letter);
fclose(file);
return 0;
}
int findWord(FILE *file, const char letter)
{
char str[BUFFER_SIZE];
while ((fgets(str, BUFFER_SIZE, file)) != NULL)
{
printf("Looking for '%c' in %s\n", letter, str);
char *c = strchr(str, (int)letter);
if (c != NULL)
{
printf("'%c' found in %s\n", *c, str);
}
}
return 0;
}
In the same directory of the source code, created afile.txt file and entered the file name. Here is the output for the program:
Enter file path: afile.txt
Enter letter to search in file: e
Looking for 'e' in This file contains some text.
'e' found in This file contains some text.
Looking for 'e' in This file is used for testing purposes.
'e' found in This file is used for testing purposes.
Looking for 'e' in This file must be read only.
'e' found in This file must be read only.
And here is the afile.txt file content:
This file contains some text.
This file is used for testing purposes.
This file must be read only.
You are encouraged to split a line of string into the words and search the letter in the word list.
Here is an example.
#include <stdio.h>
#include <stdlib.h>
#include <mem.h>
#define BUFFER_SIZE 1000
void findWord(FILE *file , const char *letter);
int main()
{
FILE *file;
char path[100];
char letter[2];
printf("Enter file path: ");
scanf("%s", path);
printf("Enter letter to search in file:");
scanf("%s", letter);
file = fopen(path, "r");
if (file == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write priveleges.\n");
exit(EXIT_FAILURE);
}
findWord(file, letter);
fclose(file);
return 0;
}
void findWord(FILE *file, const char *letter)
{
char str[BUFFER_SIZE];
do
{
fscanf(file, "%s", str);
if(strchr(str, letter[0]) > 0){
printf("%s\n", str);
}
}while (fgetc(file) != EOF);
}

How to scan a string and write it in a file?

I want to scan a string that a user inputs, then write it into the file (file.txt), but this doesn't seem to work for some reason
int main()
{
FILE *stream;
stream = fopen("file.txt", "w");
char str[] = { '\0 ' };
scanf("%s", &str);
fprintf(stream, "%s.\n", str);
fclose(stream);
return(0);
}
try this, should work just fine.
It did for me so it should for you too.
#include <stdio.h>
#include <string.h>
int main()
{
FILE *stream;
stream = fopen("ceva.txt", "w");
if (stream == NULL) {
perror("Failed: ");
return 1;
}
char str[250];
scanf("%249s", str);
fprintf(stream, "%s.\n", str);
fclose(stream);
return 0;}
You must change scanf with fscanf

counting the number of times a character appears in a file in a case insensitive manner using C language

The problem statement : a C program to count the number of times a character appears in the File. character is considered Case insensitive.
I have converted both the input character and character from the file to upper case so that none of the occurrence of the character goes uncounted. but when am executing this on an online editor am getting the result as "wrong answer" the editor isn`t accepting this code. what is the mistake in this code??
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main
{
FILE *fp;
char filename[20];
char character;
char compare;
int to_upper1;
int to_upper2;
int count=0;
printf("\nEnter the file name");
scanf("%s", filename);
fp = fopen(filename,"r");
if(fp == NULL)
{
exit(-1);
}
printf("\nEnter the character to be counted");
scanf("%c", &character);
to_upper1 = toupper(character);
while((compare = fgets(fp)) != EOF)
{
to_upper2 = toupper(compare);
if(to_upper1 == to_upper2)
count++;
}
printf("\nFile \'%s\' has %d instances of letter \'%c\'", filename, count, character);
return 0;
}
I found a few errors in your code, and made a few small tweaks. The errors are - not eating the "whitespace" before the character input, using fgets() instead of fgetc(), using escape characters before the ' symbols in the output text.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(void) // added arg for compilability
{
FILE *fp;
char filename[20];
char character;
int compare; // correct type for fgetc and toupper
int to_upper1;
int to_upper2;
int count=0;
printf("Enter the file name: ");
scanf("%19s", filename); // restrict length
fp = fopen(filename,"r");
if(fp == NULL)
{
printf ("Cannot open file '%s'\n", filename);
exit(-1);
}
printf("\nEnter the character to be counted: ");
scanf(" %c", &character); // filter out whitespace
to_upper1 = toupper((int)character);
while((compare = fgetc(fp)) != EOF) // changed from fgets()
{
to_upper2 = toupper(compare);
if(to_upper1 == to_upper2)
count++;
}
fclose(fp); // remember to close file!
printf("File '%s' has %d instances of letter '%c'", filename, count, character);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
FILE *fptr;
int d=0;
char c;
char ch,ck;
char b[100];
printf("Enter the file name\n");
scanf("%19s",b);
fptr=fopen(b,"r");
printf("Enter the character to be counted\n");
scanf(" %c",&c);
c=toupper(c);
if(fptr==NULL)
{
exit(-1);
}
while((ck=fgetc(fptr))!=EOF)
{
ch=toupper(ck);
if(c==ch||c==ck)
++d;
}
fclose(fptr);
printf("File '%s' has %d instances of letter '%c'.",b,d,c);
return(0);
}

Calling a Perl Script from a C Script, Sorting Text File

I'm making a program that displays a menu and offers to sort a file using multiple different languages. Currently I have a piece of code that will take in a text file, sort it, and output it to a new text file:
# include <stdio.h>
# include <string.h>
#define MAXNAMELEN 100
#define MAXLINELEN 100
#define MAXITEMS 1000
int main(int argc, char ** argv) {
FILE * infile, * outfile;
// Statically allocated -- dastardly!
char name[MAXNAMELEN];
char line[MAXLINELEN];
char lines[MAXITEMS][MAXLINELEN];
int i, items = 0;
printf("Enter a source filename: ");
fgets(name, sizeof(name), stdin);
name[strlen(name)-1] = '\0'; // strip newline
// No error checking -- ANYWHERE -- dastardly!
infile = fopen(name, "r");
while (fgets(line, sizeof(line), infile)) {
strcpy(lines[items], line);
items++;
}
qsort(lines, items, MAXLINELEN, strcmp);
printf("Enter a destination filename: ");
fgets(name, sizeof(name), stdin);
name[strlen(name)-1] = '\0'; // strip newline
outfile = fopen(name, "w");
for (i=0; i<items; i++) {
fputs(lines[i], outfile);
}
fclose(infile);
fclose(outfile);
}
I have the menu part down, but I'm having trouble implementing a way to call a Perl script to sort the file. I want the C program to do the writes to the output file, but the Perl Script to sort it. How can this be achieved? Nasty embedding is what I've found so far..
You can pipe a process with popen and then get the output with fgets:
#include <stdio.h>
#include <stdlib.h>
FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);
int main(void)
{
FILE *cmd;
char result[1024];
cmd = popen("myperlscript myparams", "r");
if (cmd == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
while (fgets(result, sizeof(result), cmd)) {
printf("%s", result);
}
pclose(cmd);
return 0;
}

undefined reference to `strlwr'

My code is like a text compressor, reading normal text and turns into numbers, every word has a number. It compiles in DevC++ but does not end, however, it does not compile in Ubuntu 13.10. I'm getting an error like in the title in Ubuntu "undefined reference to `strlwr'", my code is a little long so I am not able to post it here, but one of the error is from here:
//operatinal funcitons here
int main()
{
int i = 0, select;
char filename[50], textword[40], find[20], insert[20], delete[20];
FILE *fp, *fp2, *fp3;
printf("Enter the file name: ");
fflush(stdout);
scanf("%s", filename);
fp = fopen(filename, "r");
fp2 = fopen("text.txt", "w+");
while (fp == NULL)
{
printf("Wrong file name, please enter file name again: ");
fflush(stdout);
scanf("%s", filename);
fp = fopen(filename, "r");
}
while (!feof(fp))
{
while(fscanf(fp, "%s", textword) == 1)
{
strlwr(textword);
//some other logic
}
}
.... //main continues
strlwr() is not standard C function. Probably it's provided by one implementation while the other compiler you use don't.
You can easily implement it yourself:
#include <string.h>
#include<ctype.h>
char *strlwr(char *str)
{
unsigned char *p = (unsigned char *)str;
while (*p) {
*p = tolower((unsigned char)*p);
p++;
}
return str;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* strlwr(char* );
int main()
{
printf("Please Enter Size Of The String: \n");
int a,b;
scanf("%d",&a);
char* str;
str=(char*)malloc(sizeof(char)*a);
scanf("\n%[^\n]",str);
char* x;
x=strlwr(str);
for(b=0;x[b]!='\0';b++)
{
printf("%c",x[b]);
}
free(str);
return 0;
}
char* strlwr(char* x)
{
int b;
for(b=0;x[b]!='\0';b++)
{
if(x[b]>='A'&&x[b]<='Z')
{
x[b]=x[b]-'A'+'a';
}
}
return x;
}

Resources