My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum


Reply
 
LinkBack Thread Tools Display Modes
October 13th, 2015, 09:44 PM   #1
Senior Member
 
hyperbola's Avatar
 
Joined: Dec 2014
From: The Asymptote

Posts: 142
Thanks: 6

Math Focus: Certainty
C Programming - converting entire file contents to lowercase

I have a txt document and I wish to convert it's entire contents to lower case.

How should I go about this?

Below is the orginal 'no-case conversion' code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *infile;

int main() {
  int count;
  char filename[50], line[120];

  printf("Enter the file name: ");
  gets(filename);

  infile = fopen(filename, "r");
  if (infile == NULL) {
    printf("ERROR: cannot open %s\n", filename);
    exit(27);
  }
  count = 0;
  while (fgets(line, 120, infile) != NULL) {
    count++;
    printf("%d: %s", count, line);
  }
  fclose(infile);
}
Here I've tried using strlwr(line) but the file contents are not converted to lower case.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *infile;

int main() {
  int count;
  char filename[50], line[120];

  printf("Enter the file name: ");
  gets(filename);

  infile = fopen(filename, "r");
  if (infile == NULL) {
    printf("ERROR: cannot open %s\n", filename);
    exit(27);
  }
  count = 0;
  while (fgets(strlwr(line), 120, infile) != NULL) {
    count++;
    printf("%d: %s", count, line);
  }
  fclose(infile);
}
Thanks.
hyperbola is offline  
 
October 13th, 2015, 09:54 PM   #2
Senior Member
 
hyperbola's Avatar
 
Joined: Dec 2014
From: The Asymptote

Posts: 142
Thanks: 6

Math Focus: Certainty
No worries.

I used strcpy(copy, strlwr(line)) to copy lower case converted 'line' string into 'copy' string. This works.
hyperbola is offline  
Reply

  My Math Forum > Science Forums > Computer Science

Tags
contents, converting, entire, file, lowercase, programming



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
C programming - file read bug hyperbola Computer Science 1 October 13th, 2015 09:36 AM
Creating exe file from Mathematica notebook de_wight Math Software 1 May 17th, 2012 10:50 AM
Attach a pdf file???? bigli New Users 1 July 26th, 2007 06:07 AM





Copyright © 2018 My Math Forum. All rights reserved.