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
Click For Summary

Discussion Overview

The discussion revolves around a programming problem in C where a participant seeks to replace hyphens in a text file with spaces. The scope includes technical explanations and debugging of code related to file handling and character manipulation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares an initial code snippet but encounters issues with replacing hyphens, prompting questions about what "doesn't work" means in this context.
  • Another participant suggests that the approach of writing back to the same file may lead to complications, questioning the file pointer's position during output.
  • A later reply emphasizes that rewriting a file in place is problematic and proposes reading from one file and writing to another as a better approach.
  • Another participant presents a modified code attempt but notes that it still does not function as intended, leading to further clarification on the logic needed to replace characters correctly.
  • There is a suggestion to read characters until the end of the file and conditionally replace hyphens with spaces or output the original character.

Areas of Agreement / Disagreement

Participants generally agree that writing back to the same file can lead to issues, and there is a consensus that reading from one file and writing to another is a more effective method. However, there is no consensus on the exact implementation details or the best approach to achieve the desired outcome.

Contextual Notes

Participants express uncertainty about the behavior of file pointers in C and the implications of using the "r+" mode for file operations. There are also unresolved questions about the logic flow in the provided code snippets.

Who May Find This Useful

Individuals interested in C programming, particularly those dealing with file I/O operations and character manipulation, may find this discussion relevant.

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 ·
Replies
2
Views
2K
Replies
15
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K