Thread: Manipulating Text Files In C!

  1. #1
    Registered User lordi's Avatar
    Join Date
    Nov 2006
    Posts
    3

    Manipulating Text Files In C!

    Hello everyone!!!As this is my first post I'll try to be quick and not frustrating!!
    I have some playlists and I just want to open them as text and change the first letter on each line (only if it is not a #) to whatever I want!!A few functions and how they work would be great!!Thanx a lot in advance and Thanx again and again!Thaaaaaaaaaaaaaaanx!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Less ! helps.

    In the meantime, read up on fopen(), fgets() and fclose()
    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.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User lordi's Avatar
    Join Date
    Nov 2006
    Posts
    3
    yes but how can I change line every time I read the first letter?!With fgetc i can read a letter but the next I'm going to read is not from the other line but next to the first one!

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You're not trying very hard -- this discourages folks from helping.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[BUFSIZ];
          while ( fgets(line, sizeof line, file) != NULL )
          {
             printf("first character on the line: '%c'\n", line[0]);
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
    first character on the line: 'M'
    first character on the line: 'J'
    first character on the line: 'J'
    first character on the line: 'T'
    first character on the line: 'B'
    first character on the line: 'M'
    */
    Help us help you by posting your attempt.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User lordi's Avatar
    Join Date
    Nov 2006
    Posts
    3
    you're right!!Sorry!thanx for helping though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-11-2008, 01:36 AM
  2. text files in c++
    By DungeonMaster in forum C++ Programming
    Replies: 5
    Last Post: 03-14-2006, 03:48 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 04:41 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM