• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

C programming question!

binsky3333

New Member
Joined
Jul 5, 2007
Messages
643 (0.10/day)
System Name Green Machine
Processor Intel e6420@ 2.13GHz
Motherboard ASUS p5n32-e -sli
Cooling 4x80mm fans
Memory 4gb G skill ddr2-800
Video Card(s) 2 EVGA Geforce 8800GTS 320MB
Storage 1 Western digital 500gb sata
Display(s) Dell e207wfp
Case Apevia x-cruiser green
Audio Device(s) X-fi Xtreme gamer
Power Supply BFG 800watt
Software Windows vista ultimate 64bit
Ok hi everyone, i am currently making a very simple C word processor where you can type in the file name of a text file you want to open, then add text to, though i am having some problems with opening a file. Here is what i have so far:

Code:
#include <stdio.h>


int main()
{
    char write[256];
    char open[128];
    printf("Please enter the name of the file you want to open.(Does not need to exsist)\n");
    fgets ( open, 128, stdin );
    FILE *fp;
    fp=fopen("%s", "a+w", open);
    printf("Please enter the text you would like to write to the file\n");
    fgets ( write, 256, stdin ); 
    fprintf(fp, "%s\n", write);
    printf("Complete\n");
    getchar();
    
}

The problem i am having is with the fopen("%s", "a+w", open). The program will not compile because of this... I get... 178 C:\Dev-Cpp\include\stdio.h too many arguments to function `FILE* fopen(const char*, const char*)' . Basically the %s is a reading variable input. Someone types in the filename, then it is inserted into the position of %s so the user can open the file... Why wont this work what am i doing wrong?
 
Last edited:
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
Code:
FILE * fopen(const char * filename, const char * mode);

Yer passing 1 too many variables I believe.
 

binsky3333

New Member
Joined
Jul 5, 2007
Messages
643 (0.10/day)
System Name Green Machine
Processor Intel e6420@ 2.13GHz
Motherboard ASUS p5n32-e -sli
Cooling 4x80mm fans
Memory 4gb G skill ddr2-800
Video Card(s) 2 EVGA Geforce 8800GTS 320MB
Storage 1 Western digital 500gb sata
Display(s) Dell e207wfp
Case Apevia x-cruiser green
Audio Device(s) X-fi Xtreme gamer
Power Supply BFG 800watt
Software Windows vista ultimate 64bit
How can i fix this?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Exclude the last argument:
Code:
fp=fopen("%s", "a+w");
 

binsky3333

New Member
Joined
Jul 5, 2007
Messages
643 (0.10/day)
System Name Green Machine
Processor Intel e6420@ 2.13GHz
Motherboard ASUS p5n32-e -sli
Cooling 4x80mm fans
Memory 4gb G skill ddr2-800
Video Card(s) 2 EVGA Geforce 8800GTS 320MB
Storage 1 Western digital 500gb sata
Display(s) Dell e207wfp
Case Apevia x-cruiser green
Audio Device(s) X-fi Xtreme gamer
Power Supply BFG 800watt
Software Windows vista ultimate 64bit
Yes... i kno that, but then the user cannot enter the file they would like to open/create. The name is the file comes up as %s, not as what they entered.
 
Last edited:
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
Aren't you already setting it to the variable open? You're essentially trying to call fopen(filename, mode, filename);

At least that's what I believe is happening. In php, I would use fopen like:

PHP:
$fn = 'tehfilename.txt';
$fp = fopen($fn, 'a+t');
$txt = 'this is some text!';
fwrite($fp, $txt);
fclose();
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Try
Code:
fp=fopen(open, "a+w");
 

binsky3333

New Member
Joined
Jul 5, 2007
Messages
643 (0.10/day)
System Name Green Machine
Processor Intel e6420@ 2.13GHz
Motherboard ASUS p5n32-e -sli
Cooling 4x80mm fans
Memory 4gb G skill ddr2-800
Video Card(s) 2 EVGA Geforce 8800GTS 320MB
Storage 1 Western digital 500gb sata
Display(s) Dell e207wfp
Case Apevia x-cruiser green
Audio Device(s) X-fi Xtreme gamer
Power Supply BFG 800watt
Software Windows vista ultimate 64bit
Thanks that worked!
 

binsky3333

New Member
Joined
Jul 5, 2007
Messages
643 (0.10/day)
System Name Green Machine
Processor Intel e6420@ 2.13GHz
Motherboard ASUS p5n32-e -sli
Cooling 4x80mm fans
Memory 4gb G skill ddr2-800
Video Card(s) 2 EVGA Geforce 8800GTS 320MB
Storage 1 Western digital 500gb sata
Display(s) Dell e207wfp
Case Apevia x-cruiser green
Audio Device(s) X-fi Xtreme gamer
Power Supply BFG 800watt
Software Windows vista ultimate 64bit
Ut oh guys... It looks like i have another problem...

Here is my code:
Code:
int viewer()
{
   
  FILE *file;
  char c;
  char fileparam[80];
  char write[256];
  printf("Type the filename (this will create a new file or edit an existing one) = ");
  scanf("%s", fileparam);
  file = fopen(fileparam, "a+w");
  if(file==NULL) {
    printf("Error: can't open file.\n");
    /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    return 1;
  }
  else {
    printf("File opened successfully\nContents:\n\n");
    
    while(1) {     /* keep looping... */
      c = fgetc(file);
      if(c!=EOF) {
        printf("%c", c);  
        /* print the file one character at a time */
      }
      else {
        break;     /* ...break when EOF is reached */
      }
    }
    printf("\n\n");

  getchar();
  printf("Please enter the text you would like to add to the file:");
  fgets( write, 256, stdin );
  fprintf(file, "%s", write);
  printf("successful!\n");
  system("pause");
  apps();
    
}
}
Now the problem i am having is with the apps(); . Whenever i have this in the code, the text that is supposed to be written to the file is not... But whenever i take out apps(); the text is written to the file, but then the program stops because its the end of it... the apps(); is redirecting it to another function in another header file. How can i fix this problem?

EDIT: Nevermind i fixed the problem i just had to add fclose(file);
 
Last edited:

MrSeanKon

New Member
Joined
Nov 14, 2006
Messages
267 (0.04/day)
Location
Athens in love with Anna :)
Although I am not a professional programmer (I just start programming for making some cardgames + OcBible) always when you open a file for reading/writing you must close it otherwise crashes or abnornal "behavior" of your programs will occur.
As you said you found your bug :)
 
Top