Hello,

I am only starting to design a C program that opens a file;

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

int main(void)
{
      FILE *fp;      
      fp = fopen ("file.txt", "r");
            if (fp == NULL)
            {
               perror("Can't open the file");
               return EXIT_FAILURE;
            }
      printf ("%d\n", fp);
      system("pause");
      return EXIT_SUCCESS;
}

The file is a text says "the quick brown fox jumps over the lazy dog", but when I ran the program, it showed some random number than I expected :confused: How do you run the program to open the file within the terminal?

My goal is to replace a character, and the option must be supplied via command line arguments.

For example, the original text file shows
"the quick brown fox jumps over the lazy dog"
and if the program runs, using the following command line:
"assignment1 abc DEF input"
to replace from a to D, b > E, c > F
then the file should have the following contents:
"the quiFk Erown fox jumps over the lDzy dog"

It would be great if you give me an example of codes for those functions.

The file is a text says "the quick brown fox jumps over the lazy dog", but when I ran the program, it showed some random number than I expected

No, it showed a specific number -- the value of fp which is not and integer displayed as if it is an integer. In other words, a completely useless value to you. What are you expecting?

My goal is to replace a character...

Then you might want to read the file, and display what was read. That's a much more useful test.

It would be great if you give me an example of codes for those functions.

Sure it would. Then you wouldn't have to write it. Sorry, not gonna happen.

No, it showed a specific number -- the value of fp which is not and integer displayed as if it is an integer. In other words, a completely useless value to you. What are you expecting?


Then you might want to read the file, and display what was read. That's a much more useful test.


Sure it would. Then you wouldn't have to write it. Sorry, not gonna happen.

Okay. I am trying to work on it now and will post the code if something goes wrong. Um I wanted to ask you that when I open the terminal (on Unix). I typed cd Desktop. Then how do I run the program, like

> cc assig1.cpp
> a./out

do you know the difference between C and C++?

Also to run the executable you have to type
./a.out

google for the functions fread, fwrite... This will help you get started

take a file pointer
open file and

file* fp=fopen("FILENAME","r");
while(fp!=null)
{
  if(fp=='D')
  {
  //replace it with whatever you want 
  }
 fp++;
}
commented: couldn't possibly be more wrong. -1
commented: Rubbish! Go read a book about C if you pretend to be correct. -2

<FAIL>

you obviously have not attempted to try such a "solution"

because if you had, you would see that it fails miserably.

you obviously have not attempted to try such a "solution"

because if you had, you would see that it fails miserably.

Is my procedure wrong or my syntax or both ??

Your syntax is correct. But your procedure is HORRIBLY wrong. In one of my earlier posts I told you to google for some specific functions. Have you done that ?

commented: Yes. +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.