I am a student taking an introductory C course and have our first C midterm coming up. Our test environment would store our actions and printf output to a text file. However, our TA suggested we write to a file ourselves using fprintf just in-case.
Is there a very simple way I can copy my terminal/console output and input (what I enter in after scanf) to a text file like output.txt?
I tried
freopen("output.txt","w",stdout);
but that won't write my scanf input to the text file.
Can anyone help?
Don't use scanf();
Use fgets();
An example:
#include <stdio.h>
#include <stdlib.h>
#define Contents_Size 1000
int main()
{
char contents[Contents_Size];
//Opening the file
FILE * fp;
fp = fopen("\myfile.txt", "w"); //"w" = write
//If there is an error
if(fp == NULL)
{
//Exit
printf("Error!\n");
exit(EXIT_FAILURE);
}
//This part require your input
printf("Enter the contents of file: \n");
fgets(contents, Contents_Size, stdin);
//Write your input in file
fputs(contents, fp);
//Close the file
fclose(fp);
return 0;
}
fgest() will copy your input in contents[] and fputs() will paste every char of contents[] in your file.
Related
So I've written a program that will take in a information about a dvd (specifically it's postion, IDkey(just some random number) Title, Genre and Year of release), and using a struct it will write that info to a .txt file called "person.txt". I'm positive my code works for the most part but when I go to test it the output received in the .txt file is written in some weird symbol language and not English and quite frankly I have no idea as to why this is. Any explanation on why this is happening would be much appreciated, thanks :)
PROGRAM
#include <stdio.h>
#include <stdlib.h>
// a struct to read and write
struct dvd
{
int fposition;
int fIdKey;
char ftitle[50];
char fgenre[50];
int fyear;
};
int main ()
{
FILE *outfile;
struct dvd input;
// open file for writing
outfile = fopen ("person.txt", "w");
if (outfile == NULL)
{
fprintf(stderr, "\nError opend file\n");
exit (1);
}
printf("Postion: ");
scanf("%d", &input.fposition);
printf("ID Key: ");
scanf("%d", &input.fIdKey);
printf("Title: ");
scanf("%s",&input.ftitle);
printf("Genre: ");
scanf("%s", &input.fgenre);
printf("Year: ");
scanf("%d", &input.fyear);
// write struct to file
fwrite (&input, sizeof(struct dvd), 1, outfile);
if(fwrite != 0)
printf("contents to file written successfully !\n");
else
printf("error writing file !\n");
// close file
fclose (outfile);
return 0;
}
TEST RUN
TEST RUN OUTPUT IN THE .TXT FILE
You are writing these values to the file:
int fposition;
int fIdKey;
char ftitle[50];
char fgenre[50];
int fyear;
But you are displaying the whole file as characters. That kind of works for ftitle and fgenre because they really are characters...though since you don't populate all 50 characters there are some ugly uninitialized characters shown as well. That is easy to fix: just fill the unused characters (as well as the null terminator) with some known character (such as space) before writing to the file, or do not write the unused characters at all. You can use strlen() to find the length of each string and memset() to set the unused characters to a well-known character which is printable.
Next, saving an int and reading it as text is problematic. You need to decide on a single format. Either you write as integers like now, and you read as integers (which means you need a special program to read the file), or you commit to writing only text to the file.
Easiest might be to only write text to the file. You can use fprintf() for that, instead of fwrite(). You can use fprintf() for the character arrays as well, it will automatically write only the "used" part of each string up to the null terminator, skipping all the "garbage" characters.
I'm learning C and how to read/write based on file contents, but I'm having some difficulty.
Currently in this test program, I'm attempting to read from the file test.txt line-by-line without specifying the max length of the line, so basically I can't use fgets.
Instead, I try to use fscanf. But 1) I still have to specify an overbound for the length of the char array and 2) The program doesn't even work -- all it reads is the very first letter of the first line.
What am I doing wrong and how can I change the program so I don't have a max bound on length of each line (in this example its 1000)?
#include <stdio.h>
#include <stdlib.h> // For exit() function
int main() {
char c[1000];
FILE *test;
if ((test = fopen("test.txt", "r")) == NULL) {
printf("Error! Couldn't find file");
exit(1);
}
fscanf(test, "%[^\n]", c);
printf("Data from the file:\n%s", c);
fclose(test);
return 0;
}
for reference here's my current test.txt:
abcd
efgh
ijkl
mnop
qrst
uvwx yz
This question already has an answer here:
what's the preferred library for CSV parsing/writing in C++? [closed]
(1 answer)
Closed 7 years ago.
Good day. Don't know whether this question has been asked before. Any who, I have a text file with contents like below
AP0003;Football;13.50;90
AP0004;Skateboard;49.90;30
It is basically,
Item Code;Item Name;Price per unit;Quantity
I am trying to put the contents of the text file into an array but I've had no luck so far. And, I can't find anything similar on Stack Overflow (or maybe my search parameters is not accurate). Would appreciate any help I can get. Am new to C Programming.
Firstly open the file using fopen:
FILE* fp = fopen("NAME_OF_FILE.txt", "r"); // "r" stands for reading
Now, check if it opened
if(fp == NULL) //If fopen failed
{
printf("fopen failed to open the file\n");
exit(-1); //Exit program
}
Suppose that these are your arrays to store the line and each data are:
char line[2048]; //To store the each line
char itemCode[50];
char item[50];
double price;
int quantity; //Variables to store data
Read the file using fgets. It consumes line by line. Put it in a loop which terminates when fgets returns NULL to scan the whole file line by line. Then extract data from the scanned line using sscanf. It, in this case, will return 4 if successful:
while(fgets(line, sizeof(line), fp) != NULL) //while fgets does not fail to scan a line
{
if(sscanf(line, "%[^;];%[^;];%lf;%d", itemCode, item, price, quantity) != 4) //If sscanf failed to scan everything from the scanned line
//%[^;] scans everything until a ';'
//%lf scans a double
//%d scans an int
//Better to use `"%49[^;];%49[^;];%lf;%d"` to prevent buffer overflows
{
printf("Bad line detected\n");
exit(-1); //Exit the program
}
printf("ItemCode=%s\n", itemCode);
printf("Item=%s\n", item);
printf("price=%f\n", price);
printf("Quantity=%d\n\n", quantity); //Print scanned items
}
Finally, close the file using fclose:
fclose(fp);
You can try this code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str1[1000],ch;
int i=0;
FILE *fp;
fp = fopen ("file.txt", "r"); //name of the file is file.txt
while(1)
{
fscanf(fp,"%c",&ch);
if(ch==EOF) break; //end of file
else str[i++]=ch; //put it in an array
}
fclose(fp);
return(0);
}
This will put your entire file into an array str including '\n' and other special characters.If you dont want the special characters put neccessary conditions in the while loop.
Trying to learn C. Want to read the first line of a text file, my code is:
#include <stdio.h>
int main()
{
FILE *in = fopen("test.txt", "rt");
// read the first line from the file
char buffer[100];
fgets(buffer, 20, in);
printf("first line of \"test.txt\": %s\n", buffer);
fclose(in);
return 0;
}
I'm doing this in xCode. I get a exc bad access error.
test.txt definitely exists. It has one line that says "this is a text file"
try this after fopen() call:
if(in == NULL){
printf("Can't read teste.txt because: %s.\n", strerror(errno));
return 1;
}
and add the headers:
#include <errno.h>
#include <string.h>
The code looks fine, so my guess is that the program is not run in the same working directory as the file. Try placing the file in, say, /tmp/test.txt and use absolute path in fopen.
You don't check if the FILE is NULL. It may not be opened for a several reasons.
Main description of the problem below, where it happens. But simply, I cannot figure out why I get error messages after asking
if (outf!=NULL){
printf("Output file already exists, overwrite (y/n):");
scanf("%c",yn);
}
Where outf is a file pointer to an existing file. Please read description halfway through code.
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>
int main() {
/* Declare file pointer */
FILE *inf;
FILE *outf;
int linenumber,linecounter=0,linepresent;
char filename[21];
char detail[21];
char linedetail[21];
char outfilename[21];
char letter,yn='y';
int position;
/*INPUT DETAILS Ask user for file name and line number*/
printf("Please enter an input filename and a linenumber: ");
//scan filename to char string and line number to int variable
scanf("%s %i",&filename,&linenumber);
/*OUTPUT DETAILS Ask user for file name, letter & position, etc*/
printf("Please enter an output filename, a letter and a position:");
scanf("%s %c %i",&outfilename,&letter,&position);
/* Open file for reading */
inf=fopen (filename,"r");
outf=fopen(outfilename,"r");
/*check that file exists*/
if (inf!=NULL) {
Up until here everything works fine!
Then I try to find out if the outf file already exists. If outf points to an existing file, it DOES print "Output file already exists, overwrite (y/n):"
HOWEVER, as soon as it prints this I get error windows opening! This is probably an extremely rookie mistake - I'm still learning C. If there is no such file the program completes normally and bypasses the if statement okay.
if (outf!=NULL){
printf("Output file already exists, overwrite (y/n):");
scanf("%c",yn);
}
if (yn=='y'){
/*keep reading to end of file*/
while (feof(inf)==0) {
linecounter++;
/*read each line and store the line number in detail[WORDS GO HERE]*/
fscanf (inf,"%s", &detail);
/*If we reach the line selected by the user*/
if (linecounter==linenumber){
strcpy(linedetail,detail);
linepresent=1;
}
}
if (linepresent==0) {
printf("File only contains %i lines",linecounter);
}
} else {
exit(1);
}
} else {
printf("Input file not found");
}
printf("%s",linedetail);
/* close the file */
fclose(inf);
fclose(outf);
return (0);
}
First, already mentioned problems: You're opening the output file in reading mode. To open it for writing:
outf=fopen(outfilename,"w"); /* Note the "w". */
Also, scanf() accepts pointers to variables, not their values, so if you write scanf("%c", yn);, you will give scanf the character y as a pointer, which is nonsense. You need to do it like this: scanf("%c", &yn);.
Even if you fix these, however, your program won't do what you expect. If the file you're trying to open for writing doesn't exist, fopen() won't return NULL, it will create a new file. Your code will always overwrite the output file if it exists. NULL is returned only if fopen couldn't open/create the file (e.g. you didn't have the permissions to do it), and you should handle it like this:
outf=fopen(outfilename, "w");
if(outf == NULL) {
perror("Failed to open output file: ");
fclose(inf); /* Don't leave files opened. It's bad form. */
exit(1);
}
/* Open succeeded, do your stuff here. */
Note that no else block is needed after the if, because exit() ends the program immediately.
Also, there is no such thing as a "pointer to a file". FILE is just a structure that represents an open file.
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
You are open the output file with the read flag. Try changing it to "w".
outf=fopen(outfilename,"w");
Although it is worth noting writing to a file opened with "w" will whack the old file. use "a" to append to the file.
You should pass the address of yn to scanf function.
scanf("%c", &yn);