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

  • Thread starter James889
  • Start date
  • Tags
    File
In summary, you are trying to write a program that reads the content of a textfile and removes all hyphen by replacing them with ' '.
  • #1
James889
192
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
  • #2
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?
 
  • #3
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?
 
  • #4
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).
 
  • #5
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 :(
 
  • #6
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
 

1. How do I read from a file in C?

To read from a file in C, you can use the fopen() function to open the file and fscanf() or fgets() to read the contents of the file. Remember to close the file using fclose() when you are finished reading.

2. Can I use the scanf() function to read from a file in C instead of fscanf()?

Yes, you can use scanf() to read from a file in C. However, scanf() reads from standard input by default, so you will need to use the fscanf() function to specify the file you want to read from.

3. How do I replace specific characters in a file using C?

To replace specific characters in a file using C, you can use the fseek() function to move to the location of the character you want to replace, and then use the fputc() function to write the new character to that location. Repeat this process for each character you want to replace.

4. Is it possible to read and replace specific lines in a file using C?

Yes, it is possible to read and replace specific lines in a file using C. You can use fgets() to read each line of the file, and then use string manipulation functions like strncpy() to replace specific lines with new content.

5. How can I ensure that my program gracefully handles errors while reading from a file in C?

You can use feof() to check for the end of the file and ferror() to check for any errors that may have occurred while reading from the file. It is also a good idea to use fseek() and ftell() to double-check the current position in the file and make sure it matches your intended location.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top