How Can I Replace Hyphens with Spaces When Reading from a File in C?

  • Thread starter Thread starter James889
  • Start date Start date
  • Tags Tags
    File
AI Thread Summary
The discussion focuses on a C programming problem where the user wants to replace hyphens with spaces while reading from a file. Initial attempts to modify the file in place using the "r+" mode fail because the output overwrites subsequent characters. Participants suggest reading from the input file and writing to a separate output file instead, which is a safer and more effective approach. The user later tries this method but still encounters issues, indicating a misunderstanding of the logic needed to replace characters correctly. Ultimately, the solution involves reading each character and conditionally replacing hyphens with spaces before writing to the output file.
James889
Messages
190
Reaction score
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:
#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?
 
Physics news on Phys.org
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?
 
mgb_phys said:
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?

1. Doesn't replace '-'.
2. When i tried outputting to a different file the character appeared last.
3. I dunno, is there?
 
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).
 
Hi,

So i tried another approach.

Code:
#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 :(
 
Close.
Firstly that's 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
 

Similar threads

Replies
2
Views
2K
Replies
7
Views
2K
Replies
15
Views
3K
Replies
8
Views
2K
Replies
15
Views
2K
Back
Top