1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C, read from file and replace

  1. Jul 19, 2010 #1
    Hi,

    I'm trying to write a simple program that reads the content of a textfile and removes all hyphen by replacing them with ' '

    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>

    int main(){

    FILE *fp = fopen("Thetextfile.txt","r+") /* Open for reading  and writing */

    char ch;

    while(ch = fgetc(fp) != EOF){

    if(ch == '-')
    fputc(' ',fp) /*This does not work */
    }
    return 0;
    }
     
    Any ideas?
     
  2. jcsd
  3. Jul 19, 2010 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    1, What do you mean by doesn't work? Doesn't compile, crashes, doesn't replace '-'?

    2, Think out on paper exactly what you are doing. You are reading a character but only outputting a character, only if there is a match. Where is the position in the file when you output this character?

    3, Is there an easier way to do this than write back to the same file?
     
  4. Jul 19, 2010 #3
    1. Doesn't replace '-'.
    2. When i tried outputting to a different file the character appeared last.
    3. I dunno, is there?
     
  5. Jul 19, 2010 #4

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Step through the program on paper.
    You read character 1 from the file
    If it is a '-' you output a ' '. Where is the file pointer? Where does the character get outputted? You have already gone past the first character (the '-') ? Does it get printed over the second character, or at the start of the file, or at the end? Rewriting a file in place like this is such a bad idea that it's so rare I have no idea what the standard for "r+" does.

    Unless you homework specifically says so, it would be better to read a file in one char at a time and write out the result to a new file, replacing '-' chars when found.
    If you actually had to do this (replace in situ) in the real world, you would use a different method (mapped files).
     
  6. Jul 20, 2010 #5
    Hi,

    So i tried another approach.

    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>


    int main(){

    char ch;

    FILE *input = fopen("file_for_reading","r");
    FILE *output = fopen ("file_for_output","w");

    while( ch = fgetc(input) != EOF && ch != '-')
    fputc(ch,output);


    return 0;
     
    But strangely this doesn't work either :(
     
  7. Jul 20, 2010 #6

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Close.
    Firstly thats a much better way to do it, especially for testing since you don't clobber your input file.
    But you aren't doing quite what you want.

    read char from file while not end of file
    if char is a '-' print a ' '
    else print the char you read
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C, read from file and replace
Loading...