dozgunay
dozgunay

Reputation: 31

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;
}

Upvotes: 0

Views: 68

Answers (2)

RomMer
RomMer

Reputation: 1089

#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

Upvotes: 0

Ajay Brahmakshatriya
Ajay Brahmakshatriya

Reputation: 9203

Multiple issues in your code which together cause the issues

  1. 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
  2. You perform the capitalization operation outside what was set by scanf. As suggested by @MOehm, change the loop condition to s[i]
  3. 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);

Upvotes: 1

Related Questions