Thread: Problem with files in C

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Problem with files in C

    Hi.. I'm new here and i would be very happy if someone answer this
    I have a this problem:
    Make program which will make two new files: letter.txt which would include only letters and numbers.txt which would include only numbers. The primary file should content this text: Ova e 1pos56ledn7ata labo7rat8oriska vezba 93po1 pr84edm5et0ot Strukturirano 569 prog12ram4ira5nje. Se2 na1deva2me deka bese 358 in197tere8sno pi1su59vanje3to na pro22gra3mi3 vo C.
    ATTENTION: some of the words might content only numbers or only letters.
    for example: 1pos56ledn7ata--> 1567 in numbers.txt, poslednata in letters.txt

    Thank you in adition.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Read this link.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A development process
    Start with a program which simply copies input.txt to output.txt one character at a time.

    When you can do that, it's not too hard to be able to write characters to two different files, based on some test of the character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Write it as pseudocode first

    Read one character c
    Is c a letter? If so, write it out to "letters.txt"
    Is c a number? If so, write it out to "numbers.txt"
    Is c a space? If so, do nothing.
    Is c something else? Exit program with error message "INVALID INPUT: Must contain only letters, numbers and spaces"

    Next try to find out how exactly to do each thing required... "how do I read a character?", "how do I tell if c is a letter", and so on... This method works in any programming language.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    The functions you need are fopen(), fclose(), fgetc(), fputc(), isalpha() and isdigit().
    You'll need to look these up, and work out the logic yourself.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still a problem with txt files
    By Makaila in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2010, 02:11 AM
  2. Files problem
    By Signpost in forum C Programming
    Replies: 10
    Last Post: 11-29-2007, 03:23 AM
  3. Problem with .cfg files
    By ShadowIII in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2004, 06:52 PM
  4. problem with .avi files
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-18-2003, 11:41 PM
  5. I have a problem if FILEs
    By talal*c in forum C Programming
    Replies: 22
    Last Post: 01-07-2002, 05:56 AM