Thread: Need help with .txt file conversion

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    7

    Question Need help with .txt file conversion

    Hello !
    I want to write a program, that reads from an existing .txt file
    all capitals and convert them into small letters. After every sentence there should be a line-makeup and in one line there should be max. 80 characters....


    Thats is the beginning of my code:

    #include <stdio.h>

    int main() {

    FILE *f;
    f=fopen("test.txt","r");
    if (f==NULL)
    printf("Fehler beim Öffnen");
    ...
    ...
    ...

    fclose(f);

    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So what exactly do you want help with?

    To get you started, here's some case conversion information:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    Hi Hammer,
    I think you hadn't understand me...Im a C newbie (sorry for my bad american english) and the prog I have to code is not very useful, I program a little bit C in school.

    Here's my question again: (I hope it's better now)

    I want to write a program, that reads from an existing .txt textfile
    with a story all capitals like T from Tree and B from Bear and convert them into small letters to tree and bear.

    Another thing must the programm do:
    After every sentence there should be a line-makeup (\n) and in one line there should be max. 80 characters....

    Thanks for help....

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so do what you know how, and show us where you get stuck. In a nutshell, you want something like:
    Code:
    read into buffer
        convert to lower case
        do whatever
    If you're going to be reading entire lines, which you know will only ever be 80 characters max, then create a buffer of 80 characters, and read into it. Something like fgets is commonly used. Read until you're done with the file, processing the buffer between each read.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    hi again...
    can't you code it, PLEASE ?

  6. #6
    Banned
    Join Date
    May 2003
    Posts
    124
    Thats is the beginning of my code:

    #include <stdio.h>

    int main() {

    FILE *f;
    f=fopen("test.txt","r");
    if (f==NULL)
    printf("Fehler beim Öffnen");
    ...
    ...
    ...

    fclose(f);

    return 0;
    }

    Wow!!!! Really cool beginning!

    Serious now:
    Well, you have to read each character seperatily. You test if it is uppercase. If it is not capital you print it back unchanged, but if it is capital you make it lowercase and print it back. This is the first part which is easy. ( use functions: isupper, tolower )

    Secondly:
    f=fopen("test.txt","r");
    should be:
    f=fopen("test.txt","w+");

    Third:
    After every sentence there should be a line-makeup (\n) and in one line there should be max. 80 characters....
    About the 80 characters we don't care. What should we do if there is a line with more 80 charcters? I don't know. You tell me nothing anout that... Besides, in my example you take charcter by character.
    About the newline: You just test if the current character is '.'. If it is, you print back 2 characters: '.' , '\n'.

    I hope i solved all your questions.
    Good luck

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    Originally posted by korken
    hi again...
    can't you code it, PLEASE ?
    Oh my, oh my.....please tell me you're kidding!! We're not here to do your work but we will help you.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    Thanks for the comments. How can use this code with every textfile now ? When I write: programname < example.txt in my console.

    PHP Code:
    #include <stdio.h> 
    #include <ctype.h> 

    int main(void)
    {
       
    char hello[] = "Hello World";
       
    char *p;
       
       
    printf ("Before conversion: %s\n"hello);

       for (
    hello; *!= '\0'; ++p)
       {
           *
    tolower(*p);
           ++
    p;
       }

       
    printf ("After conversion: %s\n"hello);

       return 
    0;   


  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    31
    Hi
    Your code has two mistakes
    u have write ++p twice and this will cause the pionter to jump to time before make convert
    here the code after editing


    #include <stdio.h>
    #include <ctype.h>

    int main(void)
    {
    char hello[] = "HELLO WORLD";
    char *p;

    printf ("Before conversion: %s\n", hello);

    for (p = hello; *p != '\0'; ++p)
    {
    *p = tolower(*p);

    }

    printf ("After conversion: %s\n", hello);

    return 0;
    }



    out put :

    Before conversion: HELLO WORLD
    After conversion: hello world
    Press any key to continue

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Dragon227Slayer, your code also has a problem. It isn't surrounded by [code] tags.
    Away.

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    one question: how can i make a new line after 80 characters ? with \n ???

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    buffer[80] = '\n';

    You would of course have to have a buffer of at least 82 characters so you could include a null. (Assuming you actually want to treat these as strings.)

    Naturally you'd want to do it the "right" way and back up to the first whitespace, and use that, copying the remainder to the next buffer...

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    thx to all and greetings from the other side of the ocean

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM