Learning the syntax of files in C [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I can't understand the syntax of writing in files in C.
I was requested in class to open a file named Numbers.txt and then print from 1 to 10 and each new line. I dont understand the syntax of it.
We use fopen,fwrite,fclose.
need help with understanding how to work with it.

You use a FILE to assign a file to something you can use. fopen opens a file depending on if the file exists or not.
FILE *pFile = fopen("Filename.foo","r");
This opens the file that is located in your project with the same filename. The 2nd parameter is the mode of the file — how you want to use it. Here is a table of modes. You must always check the return value to ensure it is not NULL, indicating that the program was unable to open the file as requested.
You always want to close the file when you're done editing it by using fclose.
fclose(pFile);
fwrite is one way of writing to a file; fprintf and family are another. Basically think about it like you opened a text editor and typed something in the file. You need to create a buffer (a place to store the numbers), then write the numbers into the file. Here is the reference for fwrite. I can't do all of your homework.

Related

Linux script in C get file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have own script in C for linux, it is working with string loaded from file. I compiled on server gcc -pthread -o pipeline pipeline.c. It is working. Now I am running this script any like this ./pipeline UPPERCASE LOWERCASE < my_file.txt. I can read all arguments but I dont know how to read file name behind < in this script. It is possible, or how is it working?
The contents of that file will show up on your process's standard input (stdin). You don't get the name of the file, as the same interface will be used in situations where there's no filename, such as when the output of another process is piped to yours (doSomething | pipeline UPPERCASE LOWERCASE), or when the user's terminal is used for input by default.

What file extensions can be opened with c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
what types of file extensions can be opened with c
I know we can use .dat and .txt but what more can be used?
What types can be modified?
can I actually edit HTML, C++.
I'd like to open system 32.
An extension simply a convenient and conventional embellishment to a file name.
Any file can be opened with c (subject to permissioning).
You might want to choose between "text" and "binary" formats accordingly. But we're into specifics at this point, and such choices are down to you.
You can open any file extension in C. Only if you have permissions. Also, Bathsbeba answer is correct, just check out this link to see what are the main types of extensions.
To open a file with any extension, do this:
fopen ("file.extension", "your mode");
You can open it in binary and text formats. If you do not know how to open in binary, this is an example:
fopen ("file.extension", "wb");
To open in write binary mode.

What is mode of file pointer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What's mode pointer is what mode, 'insert' or overwrite?
Because I'm new to file pointer in C, so maybe my question is quite stupid. I'm sorry if anyone feels that.
And what I could do if I want to insert some character into existen file but don't lose any character in this file.
No file systems I know of support insert mode.
The C semantics for writing to file streams is either:
truncation for "w". The current file contents is lost upon successful fopen().
overwriting file contents for "r+". Only byte written explicitly to the file will overwrite existing contents, the rest is unchanged. Writing beyond the end of the file will extend its size, just like append mode.
appending to file for "a". Every write operation occurs at the end of file.
Note that "w" and "a" will also create the file is it does not exist.
In order to insert contents in the middle of an existing file, you must copy the contents to a new file, writing the new contents at the appropriate positions. You can rename the new file to the old name after closing both and removing the old one.
Depends on how you open it:
To append:
fopen("myfile.txt", "a");
To write (overwrite):
fopen("myfile.txt", "w");

Stdin , stdout, stderr, how can I get my program to take input from a text file and output to a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In a programming competition , they mentioned this:
" How to answer to the problems?
In the problems you are up to solve, you have to read data from a file .IN, and write the results in a file OUT. "
I am used to the regular way of programming, writing the code in IDE , compiling , then executing it to see what's going on, and it worked, however my answers were refused.
Does anyone know anything about those file.IN and .OUT thing?
I mean, how can I get my program to take input from a textfile in which I write the dat aI want to give to the program, and make it send the the output to another textfile ?
Thanks
Include stdio.h in your header files.
In your main function, include the following lines on top.
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
In most online programming competitions, input is given through stdin and output through stdout. However, in this case, you have to read input from a file (.in) and write to a file (.out). freopen takes the stream(stdin, stdout) specified as the third argument to re-open the stream and instead use the file specified.
Edit: Sample code takes input from input.in and writes to output.out.
#include <stdio.h>
int main(){
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}

Delete everything from file after reading it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to read the whole file and put it into an array so that I can add more to the array and sort it. Then I want to overwrite the file and add the new array of information. I was used w+ when i open the file but it overwrites it before i can even read the information. So my question is the following. Is there a way that I can read the contents from a file and then after i read everything delete everything from that file? i have the reading from and writing to the file down. It's the deleting everything inside the file I'm having a problem with.
First you need to open the file in "read" mode, Scan the data and then do the same in write mode and print the data to the same filename. When you open the filename in writemode you automatically overwrite the old file on the hdd.
Here is an example of what you can do:
FILE *inputFile, *outputFile;
// First part - Read from file
inputFile = fopen(INFILENAME, "r");
fscanf(inputFile, "Bla Bla Data", &dataFromFile);
fclose(INFILENAME);
// modify the data as needed.
// Second part. - write to same file
outputFile = fopen(OUTFILENAME, "w");
printf("bla bla new data");
fclose(OUTFILENAME);

Resources