Tuesday, July 14, 2009

How to open a file with a c program.?

Ok I tried fopen to open a file called file1.txt, but it said file1 is undeclared or something like that. What's the proper code to open a file in C (if I want to open file1.txt)?

How to open a file with a c program.?
just declare a file pointer for eg FILE* f ,then





f=fopen("drive letter:\file1.txt","mode");





mode work


"a" append can make changes at the end of file


"r" read you can just read the file cant chang in this mode


"w" write you can write any where in your file


How can I create multiple output text file in C using turbo C?

I have a text file containing number of lines (not fixed). I want to write a programme which will create separate text file containing each line. How to do this using C in Turbo C. Anybody please help me and please tell it in detail.

How can I create multiple output text file in C using turbo C?
Use the below code as a reference......





#include "stdio.h"


#include "stdlib.h"























void main( )


{


FILE *fp;


char c;


int i=1;


fp = fopen("TENLINES.TXT", "r"); // Open the file to be read





if (fp == NULL) printf("File doesn't exist\n");


else {





// perform the loop until EOF


while(!feof(fp)) {





// Read a single line.....


if(fgets(str, 126, fp)) {


printf("%s", str);


//createfile(" Call create file function from here....


................


..................








}





}











}


fclose(fp);


}








void createfile(char* filename,char* content)


{


FILE *fpwrite;





int index;


fpwrite = fopen(filename,"w"); /* open for writing */


strcpy(stuff,content);


for (index = 1; index %26lt;= 10; index++)


fprintf(fpwrite,"%s Line number %d\n", stuff, index);


fclose(fpwrite); /* close the file before ending program */


}
Reply:http://www.thescripts.com/forum/thread49...





hope this will help


Cheers:)


Compile errors in my header file? why? c++?

before in my header file i had this:


File Edit Options Buffers Tools C++ Help


#ifndef SWAP_H


#define SWAP_H


//#include %26lt;string%26gt;


//#include %26lt;iostream%26gt;


using namespace std;





void swap (string %26amp;, string %26amp;);





#endif


-------


that gave me these compile errors:


Swap.h:7: error: `string' was not declared in this scope


Swap.h:7: error: parse error before `,' token


------


now i have:


#ifndef SWAP_H


#define SWAP_H


//#include %26lt;string%26gt;


//#include %26lt;iostream%26gt;


using namespace std;





std::void swap (string %26amp;, string %26amp;);


#endif


------


Swap.h:7: error: `string' was not declared in this scope


Swap.h:7: error: parse error before `,' token


---


ALL this happens when i try to compile my Swap.cpp file.. help? wats wrong?

Compile errors in my header file? why? c++?
The compiler doesn't know what a "string" is.





uncomment the #include %26lt;string%26gt; and add


using std::string;


How to read Tiff file using C#???

I need to read a tiff file using C#, but am not getting what to do, what are steps do i need to take in .NET ( means which all dll is required to read and write and other steps).





After reading tiff file i need to trasfer the same format of tiff to TXt format.








Can any one help me... plz.............

How to read Tiff file using C#???
Read it as an array of bytes.
Reply:how to open tiff using C# Report It


wallflower

Binary Tree for searching file in c++?

I wanna write a c++ program to search inside a file and build a binary tree of all those words inside the file. Anyone can help or send me the code if you have ??





Thankx


cgsupervisor@yahoo.com

Binary Tree for searching file in c++?
Here's some code in C, but it should compile just fine


under C++ (and you can "pretty it up" with C++ method


definitions to replace the C functions if you like):





http://nob.cs.ucdavis.edu/classes/ecs030...





Also, if your problem is no more sophisticated than what


you've described, you could read the words into an array


and call qsort() in probably 20 lines of code.


I am using parser generator 2 to compile a file using C++ borland 6.?

using parser generator,when i am compiling the files with C++ borland 6,i face problem with heared and include files..and the error is:cant include %26lt;.....h%26gt; forexample..or another is:ERROR must use c++ ..or something like this.


may i kow if i should do any copy %26amp; paste files between parser and borland in include folders or..can some one help me to do the projects?i realy will be thank ful..i need to read characters from a file and define the TOKENs or lexical errors

I am using parser generator 2 to compile a file using C++ borland 6.?
Fully qualify your path names (not doc.txt, but C:\Documents and Settings\Administrator\My Documents\doc.txt). This guarantees that a "file not found" is a file that doesn't exist there.





An unsafe way around getting rid of those ERROR: Must use C++ is including (before the libraries) a #define __cplusplus line. That should work, considering that the safety responsible for that mechanism is:


#ifndef __cplusplus


#error Must use C++


#endif


What are the file read & write functions in C# ???

Actually i want to know that what are the file read %26amp; write functions in C#, like fputs(),fgets(), or fprintf(), and fscanf(); and please also mention how to open file in c# . and how to close it.......





please reply............

What are the file read %26amp; write functions in C# ???
Ok, what you're describing is C functions which the concept is expanded in C#. Several objects are used to read and write files. They include the buffered streams, file streams, text read/writers to name a few. Most fall under the system.io class or input/output class of structure. An example is demonstrated below:





using System;


using System.IO;





class FSRead


{


public static void Main()


{


//Create a file stream from an existing file.


FileInfo fi=new FileInfo("c:\\csc.txt");


FileStream fs=fi.OpenRead();





//Read 100 bytes into an array from the specified file.


int nBytes=100;


byte[] ByteArray=new byte[nBytes];


int nBytesRead=fs.Read(ByteArray, 0, nBytes);


Console.WriteLine("{0} bytes have been read from the specified file.", nBytesRead.ToString());


}


}








I've also included some links below.





Hope this helps.

hollyhock

Function for copying a file in c language?

for writing nc i should copy file from a drive and paste it in other


drive or location .what function i should use for copying a file.


and also for cuting a file.


notice:i am programing in c.

Function for copying a file in c language?
On UNIX style systems you can execute commands with the system call, for example





system("cp pathname1 pathname2")





Likewise on Windows but with copy instead of cp.





http://www.die.net/doc/linux/man/man3/sy...








Windows has a CopyFile function which can be called from C.





http://msdn.microsoft.com/library/defaul...
Reply:This is not tested and has no real error checking but should get you started.





int copy (char * src, char * dest)


{


char buf[1024];


FILE * in, *out;





in = fopen(src, "r");


if(in == NULL)


{


return(-1);


}





out = fopen(dest,"w");


if(out == NULL)


{


return(-1);


}





while(fread( buf,


sizeof(buf),


1,


in))


{


fwrite(buf,


sizeof(buf),


1,


out);


}


fclose(in);


fclose(out);


return(0);


}
Reply:Or you can have the program do it with the appropriate calls. Use C file I/O to open the file, write it to another file at the new location, close both. Then make calls to appropriate functions to delete the old files if needed. I beleive that unlink works in *nix and kill works under DOS or DOS box. You'll have to look up the Windows functions if you need it in the Win32 or Win32s APIs. I hope this helps.
Reply:DanD is quite rignt. One additional detail: you can use function "system" in DOS and Windows also.


system( "copy pathname_1 pathname_2" );


If you want to "cut" the file call then


system( "del pathname_1" ) for Windows


of


system( "rm pathname_1" ) for *NIX


Cut and Paste file from C drive to D drive?

I have a whole bunch of Sims games installed in my computer under C drive. But now my C drive is really filled to the max and my D drive has about 8GB free.





Is it possible to cut and paste the whole EA games file from C drive to D drive?

Cut and Paste file from C drive to D drive?
It might Create issues in cutting and pasting, as some files also connected to system registry and refer to original location. You start installing new files on D: drive as it instalation will give you option to decide about target folder. You can save some space in C drive by de-intalling some games or un-sed software or moving noral data(like documents, songs etc) from C to other Drive
Reply:No u can't CUT and PASTE. Instead u can re-install to your D: Drive.
Reply:Uninstall the sims, I believe it will ask you if you want to keep the saved games. Reinstall on the other drive and transfer the saved games folder.
Reply:Yes you can, you must do it in a whole folder and take patience because it my take a time to transfer a big files, after the procedure run a disk clean-up to drive C and maybe defrag it.





I hope it helps!
Reply:Try CTRL+C and CTRL+V first. Then start playing them from the D: drive. If that works, then delete from C: drive.
Reply:No, the installed game will still try to read files from their original installed location.


How can I run an exe file by C# Windows programming?

How can I run an exe file by C# Windows programming? I have problems in running an exe file by clicking the button that i made in c# windows programing, can anyone of you write the code of event handler in c# through which i can run exe file by just clicking the button,rest of the code is:


using System;


using System.Windows.Forms;


using System.Drawing;


class ButtonForm : Form {


Button MyButton = new Button();


public ButtonForm() {


Text = "Respond to a Button";


MyButton = new Button();


MyButton.Text = "Press Here";


MyButton.Location = new Point(100, 200);


// Add button event handler to list.


MyButton.Click += new EventHandler(MyButtonClick);


Controls.Add(MyButton);


}


[STAThread]


public static void Main() {


ButtonForm skel = new ButtonForm();


Application.Run(skel);


}


// Handler for MyButton.


{ Please guide me with this part.


}


}


Thank u in advance.

How can I run an exe file by C# Windows programming?
Okay, you already have a function defined, you need to go to your Form.cs file, where the EventHandler is located.





MyButton.Click += new EventHandler(MyButtonClick);





This points to the MyButtonClick function.





**************************************...


Now you need to add:





using System.Diagnostics;








Then create a process:


Process TheProcessYouNamed;





private void MyButtonClick(object sender, EventArgs e)


{


TheProcessYouNamed = Process.Start("EXEToRun.exe");








TheProcessYouNamed.CloseMainWindow();


}


File handling in C?

hi guys and gals. i've gotta submit a project. i hav to handle student details such as name, roll number,5 marks, avg,result and rank. i need to save it in a file using C. cud u plz help me gettin the codes. plz.

File handling in C?
Unless you're working with a database system (and you probably aren't), the file management is the least of your worries.





First, develop a program that handles the records in an array (or better, an STL vector or a linked list) in memory. That's where you're going to have to add records, delete records, sort records, etc. It's easier to manage it here than in a file.





Second, write code that goes through your array/list and writes each record to a file. You'll then write the opposite of that which builds a list from the file. These you'll call when your program quits and when it starts. If you want to get fancy, you might save more frequently than this in case of crashes, either on a timer or before and after sorting or other operations.





Now to the crux of your question, how to do this. That's going to depend a lot on what API you're using. In windows you'd use CreateFile and WriteFile. Using standard C or C++ with streams will give you other options.





I've added a link here to an article that goes over some of the standard C options for reading and writing files.





http://www.exforsys.com/content/view/207...
Reply:The file save fuction is differ C# then GML.





To write GML go to http://www.gamemaker.nl/download.html

cabbage

Most important file in c drive?

wat is the most important file/files in the c drive that will coz serious problems for windows to run if they are deleted by accident?

Most important file in c drive?
content of windows folder or system files like ntldr.sys....


Hi! How do I open a file in C and create one if it doesn't exist?

I have a problem with creating a file in C. I would like to open existing file (or create one if it doesn't exist) and write something into it:


/* open the file */


if ((shfd = open(argv[2], O_CREAT | O_RDWR, 0677)) %26lt; 0)


my_error("open failed");


/* map a portion of the file to buffer in memory */


if ((mem = mmap(0,SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, shfd, 0)) == (void *)-1)


my_error("mmap failed");


sprintf(mem, "%d", getpid());


However, when the file doesn't exist and is created, its empty, and I get "bus error" message at the last line... If I try to write something to the file that already exists and is empty, I get the same message. If it exists and is not empty, it works fine...


Could anyone please help? I would really appreciate any help, since I don't know why I'm getting that error...





Thanks a lot for your time.

Hi! How do I open a file in C and create one if it doesn't exist?
You can't extend a file with mmap(). You can map


memory that you might write into, but when you close


or fsync the file, it will NOT be extended.





The historical way to do this is to lseek to the position


you want and write a single byte. The filesystem will


arrange that the intervening bytes will look like zeros.





I've modified your program snippet to open a file,


mmap it, and scribble in a bunch of bytes -- while


preserving whatever is there. It's not particularly


clever, but it should show most of the details you're


looking for. You can contact me through YA if you


have questions.





#include %26lt;sys/types.h%26gt;


#include %26lt;sys/stat.h%26gt;


#include %26lt;fcntl.h%26gt;


#include %26lt;sys/stat.h%26gt;


#include %26lt;unistd.h%26gt;


#include %26lt;sys/mman.h%26gt;


#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;stdlib.h%26gt;





int


main(int argc, char* argv[])


{


  int shfd;


  struct stat statbuff;


  char *filename = argc%26gt;1 ? argv[1]   : "testfile";


  int mmapsize = argc%26gt;2 ? atoi(argv[2]) : 100000;


  char *mem;





  /* open the file */


  if ((shfd = open(filename, O_CREAT | O_RDWR, 0677)) %26lt; 0)


    perror("open failed"), exit(-1);





  /* if it is too small, we will extend the file for the mmap */


  if (fstat(shfd, %26amp;statbuff) != 0)


    perror("stat"), exit(-1);


  if (statbuff.st_size %26lt; mmapsize) {


    if (lseek(shfd, mmapsize-1, SEEK_SET) == (off_t)-1)


      perror("lseek"), exit(-1);


    if (write(shfd, "\0", 1) %26lt; 0)


      perror("write"), exit(-1);


  }





  /* map a portion of the file to buffer in memory */


  if ((mem = mmap(0, mmapsize, PROT_READ | PROT_WRITE, MAP_SHARED, shfd, 0)) == MAP_FAILED)


    perror("mmap failed"), exit(-1);





  /* scribble on the file *after* whatever was already there */


  if (statbuff.st_size %26lt; mmapsize)


    memset(mem+statbuff.st_size, 'X', mmapsize-statbuff.st_size);





  if (close(shfd) %26lt; 0)


    perror("close"), exit(-1);





  return 0;


}
Reply:Hmmm, I dont follow how you are trying to do it but this is how I always do it.





void main()


{


FILE *Pfile;


char FileName[] = "nameoffile";


char buffer[];





//open file or create if it doesnt exsist


Pfile = fopen(FileName, "w"); // w = open for write


// r = open for read


// wb = open binary for write


//fill buffer with what you want to write to file


strcat(buffer, "stuff you want to add to file");


//write buffer to the file


fwrite(%26amp;buffer, strlen(buffer), 1, Pfile);


//DONT forget to close the file when done!!


fclose(Pfile);


}


Reading data from file using C++?

I have a text file representing time-voltage signal 2 colums of numbers, in scientific format eg.


time voltage


0.003 1.553e-8


... ...





how do I read them into a mx2 matrix using C++?





used to working with C, not familar with C++

Reading data from file using C++?
You could use class ifstream.


It makes very easy to read from a file as an input stream.
Reply:If you are not required to use the C++ library, then use the C one. I always preferred the C one. Even in my college courses, where we were expected to use C++ I was not penalized for using the standard C library. If you are going to school, you might want to double check if this is acceptable. IMO the C++ standard library using too confusing and abstracted of a syntax to be understandable. Plus I knew how to look at standard C code on paper and determine what the result would be, in C++ I wasn't and still can't always do this.


Do I need to file Schedule C?

I started the business in 2006 and claimed a loss of the business income and filed a Schedule C for 2006.But, I only participated passively(less than 500 hours) in my business for 2007. I did not sell any inventory nor did I buy anymore. I used some items from the inventory for personal use. I did not have any expenses for 2007. I plan to close the business this year 2008. Do i need to file a Schedule C for 2007 and if so what line items would i need to file out?





Appreciate any help.

Do I need to file Schedule C?
I'm not sure but you do have to claim your inventory if you did not close the business prior to 2007 even if you did not sell any inventory. It is still considered an asset. I would consult with an accountant on this.
Reply:If you were in possession on inventory that you did not sell, can you not write off the depreciation?
Reply:its sounds like no you do not if you had no expenses cant see what you would fill out

phlox

How to use java file in c#?

Hai all,





I have one doubt in c#.


I have few files in java.


Can i use those java file in c#.





For example DataSheet is a class in java.


Can i use that DataSheet class in my windows application.





But that java file is opened like J#.





Then i have another doubt. What is language interoperability.


Any relation is between language interoperability and the above case.





Any one please guide.





Thanks advance

How to use java file in c#?
Use Java in C# using IVKM.NET (http://www.codeproject.com/useritems/csh...


Or this http://www.thescripts.com/forum/post1025...





Language interoperability is the ability of code to interact with code that is written using a different programming language. Language interoperability can help maximize code reuse and, therefore, improve the efficiency of the development process.





Yes, language interoperability is demonstrated in this case. See http://www.velocityreviews.com/forums/t1... for more details on how C# and Java interoperate with examples,


How can I delete a .exe file that has secretly been installed in my C: drive?

After visiting a website ZoneAlarm notified me that a .exe file in my C: drive was trying to access the internet. I know this is a foreign file b/c I never installed anything like that and nothing like that was ever there. Norton has not been able to quarantine the file b/c a message says the file is in use or write protected. I cannot delete the file for the same message. What can I do? This file is repeatedly trying to access the internet. I'm suspecting it's a malicious file.

How can I delete a .exe file that has secretly been installed in my C: drive?
Probably easiest to boot the system in safe mode (tap F8 every second or two while booting until you see the screen giving you the option to boot into safe mode). Once there, you can probably delete the file without any problems.





Failing that, try downloading Knoppix (http://www.knoppix.com). Boot off of that, and you'll definitely have the ability to delete it.





To make an effort to prevent this activity in the future, try downloading a free realtime spy/malware monitoring tool such as Windows Defender.
Reply:I had the same problem before - I honestly can't remember how I fixed it now but I would suggest you go into your task manager (ctrl-alt-del) and google each of your processes that you're not sure about - you should pick up a website that will have most of them and you can just search in there.





End any suspicious processes (the website will usually suggest whether it is recommended to end certain processes or not) and go back to the file you want to delete and try deleting it before it starts itself again.





Even if this doesn't solve your problems (there are some better solutions above), you should clear some of your ram and the pc will run faster.
Reply:write down the file name ... then click start and go to find or search, click on all files and folders then type in the file name.. let it search then right click any and all files one at a time and delete them empty your recycle bin and then rerun you virus and malwear protection
Reply:Put it on a different folder to the one it is on, and then quickly delete that folder.


If that doesn't work, you will have to boot in Safe Mode, run antivirus and antispyware software, and if the file is still there, delete it.


http://www.pchell.com/support/safemode.s...


Can I move Norton Anitivirus definition file in C:\Program Files\Common Files\Symantec Shared\Virus Defs to D:

I am using Norton Internet Security 2004 together with Norton Anitfvirus on Windows 2000 plarform. The virus definition file is growing almost everyday at speed of 50MB/day. Now the file size is 5000MB (5GB) but I am using Windows 2000 FAT32 file system. My C partition can only extend to 7.8GB. Now C partition has 400MB free space left only and is approaching risk limit . However I still have 6GB in my D partition.

Can I move Norton Anitivirus definition file in C:\Program Files\Common Files\Symantec Shared\Virus Defs to D:
Moving the virus definitions would break Norton.





Seriously, and I know you didn't want to hear this, it would greatly benefit you to dump Norton and use a different antivirus software. Norton is recognized rather widely by techs to be inefective.





A good free one (I use the paid version) is www.free-av.com, which is Avira AntiVIR. This is the best anti-virus I have ever come across, and I've test-driven many.





Good luck!


Can i take emage file of C & D drive which is carry on C= windows 98 se and D=windows XP Pro SP-2 Pl send prg.

I have four partition CDEF. C for win 98 se %26amp; D for win XP sp2.


E, F for data storage. Now i wnat to take a sing file as mirror ro emage file off both drive C %26amp; D. Pl if some one have any prog which is fullfill above facilities send me by mail in zip format. OR guide me Please Help me.......

Can i take emage file of C %26amp; D drive which is carry on C= windows 98 se and D=windows XP Pro SP-2 Pl send prg.
Can you access win 98 from xp if so bring the firle too the desktop of xp and go from there.

verbena

Do I file 2 schedule C forms if I have 2 businesses, if so how do I combine the figures from both?

I have 2 businesses. One is my main business and one is a side business. I usually file scedule C for each of them however since one of these businesses has not done any sales in previous years this was never an issue. Last year one of my businesses had a slight loss and the other one made profit. I am not totally sure what to do next. I think I am supposed to file 2 different schedule C forms and then somehow combine figures but I am not totally sure. Any suggestions? Detailed suggestions would be helpful.

Do I file 2 schedule C forms if I have 2 businesses, if so how do I combine the figures from both?
you have it exactly correct; do two separate Schedule Cs and then add the net income figures together before you go on to Schedule SE
Reply:Yes, two schedule C forms, but add up the numbers from the end to go on one schedule SE and one form 1040.





Example: if one made $8000 and the other lost $500, the total would be $7500 and that would go on top of the schedule SE and onto the 1040.
Reply:When you own an unincorporated small business by yourself, the IRS considers you a sole proprietor. Your business earnings (or losses) are included as part of your individual income tax filing.





If you run two or more sole proprietorships, you must file a Schedule C for each.





You would "net out" the profit/loss from the two to enter on your form 1040.
Reply:You are correct that 2 Schedule Cs would be completed. The combined total profit/loss would go on page 1 of your 1040.