• We've upgraded our forums. Please post any issues/requests in this thread.

C programming question!

Joined
Jul 5, 2007
Messages
643 (0.17/day)
Likes
21
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
#1
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,059 (1.07/day)
Likes
1,123
Location
Geneva, 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
#2
Code:
FILE * fopen(const char * filename, const char * mode);
Yer passing 1 too many variables I believe.
 
Joined
Jul 5, 2007
Messages
643 (0.17/day)
Likes
21
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
#3
How can i fix this?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
20,922 (6.24/day)
Likes
10,023
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#4
Exclude the last argument:
Code:
fp=fopen("%s", "a+w");
 
Joined
Jul 5, 2007
Messages
643 (0.17/day)
Likes
21
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
#5
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,059 (1.07/day)
Likes
1,123
Location
Geneva, 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
#6
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
20,922 (6.24/day)
Likes
10,023
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#7
Try
Code:
fp=fopen(open, "a+w");
 
Joined
Jul 5, 2007
Messages
643 (0.17/day)
Likes
21
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
#8
Thanks that worked!
 
Joined
Jul 5, 2007
Messages
643 (0.17/day)
Likes
21
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
#9
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.07/day)
Likes
73
Location
Athens in love with Anna :)
#10
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 :)