I don't want to see into file-output the last character - c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
FILE *fp;
if((fp=fopen("Example.txt", "r"))== NULL){
printf("Errore apertura file");
exit(0);
}
char s[50];
int i=0;
while(!feof(fp)){
if(!feof(fp)){
s[i++]=fgetc(fp);
}
}
s[i]='\0';
fclose(fp);
char nome[20];
printf("Inserisci il nome che vuoi dare al file di uscita\n");
//fgets(nome,20,stdin);
scanf("%s",& nome);
char tipo[5]=".txt";
strcat(nome,tipo);
if((fp=fopen(nome,"w"))== NULL){
printf("Errore apertura file");
exit(0);
}
fputs(s, fp);
fclose(fp);
return 0;
}
The output file over the string is even printed an abnormal character, how can I not see it?
The output is "string"+'ÿ'`
The problem is only in the output file and not in the capture.

Your input loop should be:
char s[50];
int i=0;
int c;
while (i < (50 - 1) && (c = fgetc(fp)) != EOF)
s[i++] = c;
s[i] = '\0';
Or even:
char s[50];
int i;
int c;
for (i = 0; i < (50 - 1) && (c = fgetc(fp)) != EOF; i++)
s[i] = c;
s[i] = '\0';
These avoid buffer overflow and do not try to store EOF in the array s. They also doesn't use feof(); you seldom need to use it, and when you do, it is after a loop has ended and you need to distinguish between EOF and a read error (see also ferror()).

Related

running the program doesn't print the second printf statement

whenever I run the program the second print statement isn't printing. I tried using a function but I'm new to C and don't really understand anything. I've also attached my activity as I'm not sure how to do the other things on it.activity photo
#include <stdio.h>
#include <string.h>
#define LINE_LENGTH 1000
int main(int argc, char **argv)
{
FILE *input_file = fopen("cstest.c", "r");
char line [LINE_LENGTH];
//while loop to ger
while (fgets(line, LINE_LENGTH, input_file) != NULL)
{
int ch = 0;//ch is the cast
int lines = 0;//start with one because
if (input_file == NULL)
return 0;
while (!feof(input_file))
{
ch = fgetc(input_file);
if (ch == '\n')
{
lines++;
}
}//end while
printf("lines: %d\n", lines);
}//end while loop
while (fgets(line, LINE_LENGTH, input_file) != NULL)
{
int ch = 0;//ch is the cast
int lines = 0;//start with one because
if (input_file == NULL)
return 0;
while (!feof(input_file))
{
int characters = 0;
char c;
for (c = getc(input_file); c != EOF; c = getc(input_file))
// Increment count for this character
characters = characters + 1;
printf("characters: %d\n", characters);
fclose(input_file);
}
}
}

Printing 2d Array with unknown number of strings in C

I have written a small program to read in a series of strings from a file and then to store them in a 2D Arrray. The strings are read into the array correctly, yet my program is not countering the number of rows in the file like I had expected.
I am honestly at a loss and cannot figure out why the program is not counting the rows in the file. Any explanation as to what I am doing wrong is greatly appreciated.
Here is the code that I have so far:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE* fp;
char nameArray[20][120], str[20];
int i = 0, j = 0, n;
int count = 0;
char name[20]; //filename
int ch;
printf("Please enter a file name: ");
scanf("%s", &name);
fp = fopen(name, "r");
if (fp == NULL) {
printf("File \"%s\" does not exist!\n", name);
return -1;
}
while (fscanf(fp, "%s", str) != EOF)
{
strcpy(nameArray[i], str);
i++;
}
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n')
count++;
}
for (i = 0; i<=count; i++)
{
printf("%s", nameArray[i]);
}
fclose(fp);
return 0;
}

to upper case every words in file in C

can you tell me what adjustments i can do for my code, or any simplifications? What shouldn't
i repeat, what should i change ? This code converts every word to upper case, if you find some problems,pls write in order to fix it))
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
FILE * fPtr, *fPtr1;
int c; /*to store characters*/
char filename[20];
char filename2[20] = "temp.txt";
printf("Enter name of file: ");
scanf("%19s%*c",filename);
fPtr = fopen(filename, "r");
fPtr1 = fopen(filename2, "w");
c = fgetc(fPtr);
while(c!=EOF){
if(c!='\n'){
if(islower(c)){
fputc(c-32,fPtr1);
}else{
fputc(c,fPtr1);
}
}else{
fputc(c,fPtr1);
}
c = fgetc(fPtr);
}
fclose(fPtr);
fclose(fPtr1);
remove(filename);
rename(filename2,filename);
fPtr = fopen(filename, "r");
c = fgetc(fPtr);
while(c!=EOF){
printf("%c",c);
c = fgetc(fPtr);
}
fclose(fPtr);
}
This program does what you say it does. But I recommend some changes that your future self will appreciate.
First, always initialize your variables; this habit will help to prevent odd bugs in your future code. Set ints to a value out of your expected range (e.g. maybe -1 in this case); set pointers to NULL; set char arrays to { '\0' } or to "\0".
Next, check your file pointers (fPtr, fPtr1) for NULL after fopen.
Finally, specific to this code, your check for new-line is unnecessary; islower will return 0 if the parameter is not a lowercase alphabetic character.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 20
char *mygets(char *s, size_t sz) {
int ch;
size_t i = 0;
while((ch = getchar()) != '\n' && i < sz)
s[i++] = ch;
s[i] = '\0';
return s;
}
int main(void) {
FILE *fPtr;
char filename[MAX+1];
int c, i;
printf("Enter name of file: ");
mygets(filename, MAX+1);
if(!strstr(filename, ".txt"))
strcat(filename, ".txt");
if((fPtr = fopen(filename, "r+")) == NULL) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
i = 0;
while((c = fgetc(fPtr)) != EOF) {
fseek(fPtr, i, SEEK_SET);
fputc(toupper(c), fPtr);
i++;
}
rewind(fPtr);
while((c = fgetc(fPtr)) != EOF)
putchar(c);
fclose(fPtr);
return 0;
}

Basic File IO in C Produces all a's

I am using CodeBlocks on Windows to compile.
Why the program gives me this answer? Why there are so much as and don't get the answer 123456abcdef?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
char s[100] = "abcdef";
char c1 = '0';
int i = 0;
fp = fopen("ot.txt", "w");
if (fp == NULL) {
printf("file open error");
exit(0);
}
while (s[i] != '\0') {
fputc(s[i], fp);
i++;
printf("%d", i);
}
while (c1 != EOF) {
c1 = fgetc(fp);
putchar(c1);
}
fclose(fp);
}
There are multiple problems in your code:
c1 should be defined with type int to accommodate for all values returned by fgetc(). a char cannot unambiguously store EOF.
You should open the file in write+update mode "w+"
You should rewind the stream pointer before reading back from it for 2 reasons: a seek operation is required between read and write operations and you want to read the characters from the start of the file.
You need to test for EOF after reading a byte with fgetc(), otherwise you will output the EOF converted to unsigned char to stdout before exiting the loop.
It is good style to return 0; from main() to indicate success and non-zero to indicate failure.
Here is a corrected version:
#include <stdio.h>
int main(void) {
FILE *fp;
char s[] = "abcdef";
int i, c;
fp = fopen("ot.txt", "w+");
if (fp == NULL) {
printf("file open error\n");
return 1;
}
i = 0;
while (s[i] != '\0') {
fputc(s[i], fp);
i++;
printf("%d", i);
}
rewind(fp);
while ((c1 = fgetc(fp)) != EOF) {
putchar(c1);
}
printf("\n");
fclose(fp);
return 0;
}

Lowercase to uppercase with opening new file

I tried to change all random lowercase letters to uppercase letters in this program.First of all, I have initialized in lowercase.txt AkfsASlkALfdk.Then I read from it and changing all the lowercase letters into capital ones.The problem is,when I opened the capital.txt is ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌAKFSASLKALFDK.Where did the error come from?I couldn't find it yet and I decided to ask you.
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <conio.h>
int main()
{
int i;
char s[100];
char k[100];
FILE *kp;
kp = fopen("lowercase.txt", "r");
if (kp == NULL)
{
printf("Error in opening file.\n");
system("pause");
exit(1);
}
FILE *temp;
temp = fopen("capital.txt", "w");
if (kp == NULL)
{
printf("Error in opening file.\n");
system("pause");
exit(2);
}
printf("Opening file is successful.\n");
if (fscanf(kp, "%s", &s) != EOF)
{
for (i = 0; i < 100; i++)
{
if (s[i] >= 97 && s[i] <= 122)
{
s[i] -= 32;
}
}
}
fprintf(temp, "%s", k);
getch();
return 0;
}
Multiple issues in your code which together cause the issues
You are storing the opened FILE* in temp, but checking kp. I think that is because you copy pasted the check from above. Can be easily fixed by changing the variable
You perform the capitalization operation outside what was set by scanf. As suggested by #MOehm, change the loop condition to s[i]
Finally you are converting the string in place in s but are saving k in the file. k is never modified. Change fprintf(temp, "%s", k); to fprintf(temp, "%s", s);
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char *append(const char *s, char c) {
int len = strlen(s);
char buf[len+2];
strcpy(buf, s);
buf[len] = c;
buf[len + 1] = 0;
return strdup(buf);
}
int main(int argc, char **argv)
{
char ch;
FILE *fp;
if (argc != 2)
return (0);
if ((fp = fopen(argv[1], "r")) == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
char *res;
while((ch = fgetc(fp)) != EOF)
{
res = append(res, ch);
}
fclose(fp);
int i = 0;
while (i < strlen(res))
{
if (res[i] >= 97 && res[i] <= 122)
res[i] = res[i] - 32;
i++;
}
printf("%s\n", res);
return 0;
}
here is a quick example
read the file char by char and add each char in a char *. Then for each character lowercase char, sub 32 to get the uppercase char and write it then print. Give the filename as first parameter when you start the programm

Resources