ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I am new to this site. I ran into it while google Segmentation Fault. I'm writing a simple C program that reads a file that counts each line and numbers it then writes to a file called sdout. I copyed my program mostly from the text book but im still having problems. Can anyone help? I would really appricate it. Heres my code:
Hi. main takes two parameters. The correct usage is "int main( int argc, char *argv[] )" Please note that the argv argument is an array of pointers, so its usage is argv[0] for the first argument (which contains the program name), argv[1] for the second (which contains the first command line argument supplied by the user), etc. Also note, the argc parameter is an integer that contains the number of parameters in the argv[] array.
Can anyone please help? I tryed moving on to doing anoter program and i still keep getting Segmentation Fualts. I dont understand it. I looked it up and everything, I'm desprate for help! haha
Thank you so muhc for your reply, and yeah thats what it had in to book, but I have to have the file write to a predetermined file, is that possable? Or am i miss understanding something when it reads "The program should write to stdout."
Thank you so muhc for your reply, and yeah thats what it had in to book,
If you now understand that part of your errors, you should have already corrected that part of your program and retested. If you still need help, you should have posted the program with the correction you made so far.
Also please use code tags when posting code.
Quote:
but I have to have the file write to a predetermined file, is that possable? Or am i miss understanding something when it reads "The program should write to stdout."
stdout isn't exactly a file (at least in the sense of "file" implied by what you said above) and stdout is already open when your program starts. You should not try to open it as you seemed to do in your code: ofp=fopen(stout,"w");
Many output methods go directly to stdout instead of having you specify which file the output goes to. Alternately, you can use output functions in which you do specify which file, but specify stdout.
For example, the function putchar does the same thing as putc, except putchar only goes to stdout while putc goes to any FILE pointer.
Yeah, thats what it had in to book, but I have to have the file write to a predetermined file, is that possable? Or am i miss understanding something when it reads "The program should write to stdout."
Yes, you can use arguments to specify the name of the output file you want to create. I think you are confusing something about how the system works, specifically your statement about writing to stdout. By default, the system automatically creates three file descriptors for you when you execute a program. stdin, stdout, and stderr. By default, stdin is connected to the keyboard input device such that when you issue an gets() function call (or in your case multiple getc() calls using the stdin fd) you get the data typed on your keyboard. stdout by default outputs data to the terminal screen, and stderr also by default outputs text to the screen.
I say "by default" because you can use the shell "redirection" operators to redirect either stdin, stdout, and/or stderr to specifiec files or other I/O types of devices.
I would suggest that you use the program sample for found AS IS (letting it copy textual characters from stdin and outputting them to stdout) and simply redirect stdout to your new file. To do this you would do something like "./a.out > myfile" This tells the shell to redirect stdout to a file called myfile. Please read up on the shell, and shell redirection for more information.
If you want to write a program to algorithmically do the redirection then the approach you are beginning to take will work once you fix the programs you are having using main's argc/argv parameters. Please take a look at the following program:
Code:
#include <stdio.h>
int main( int argc, char *argv[] )
{
int i;
for ( i = 0; i < argc; i++ )
printf( "argument %d: %s\n", i, argv[i] );
return( 0 );
}
compile and run "./a.out" "./a.out myfile" "./a.out some additional text"
Many output methods go directly to stdout instead of having you specify which file the output goes to. Alternately, you can use output functions in which you do specify which file, but specify stdout.
Sorry, im still new haha, thank you!
So what your saying is stdout is the standard output file if none is provided? Sorry, i'm still a little confused. I fixed my code:
The program compiles fine, but when I run it it does not create a output file. I only enetering the file i wanted it to run, ex "main text" as well as i tryed entering the output to, "main text out" but it dose not create the output file. Also after it runs, it changes the file it was supposed to read from to " ELFat 3.4.6-10) " and im not sure what to make of that, any ideas?
Many output methods go directly to stdout instead of having you specify which file the output goes to. Alternately, you can use output functions in which you do specify which file, but specify stdout.
For example, the function putchar does the same thing as putc, except putchar only goes to stdout while putc goes to any FILE pointer.
(It is a C++ site, but a C section of the C++ documentation, so it does apply to C).
Thank you, I think I understand it a little better now. The line i was confused about, "The program should write to stdout." basically means to pring the information on the screen aswell? I really think my teacher should have explained this better to us.
Is your question about whether you can use printf to output the contents of a variable? If so, the answer is "yes". I assume that your book has a description of the syntax for printf.
Note on "stdout": This is the common term for the location of the default output port of the environment that you are operating in. When you run any program in a terminal, "stdout" is the terminal window---unless you (or the program) has done something to change it.
To learn more about this, read up on redirection in the BASH shell.
"The program should write to stdout." basically means to pring the information on the screen aswell?
I would expect it to mean instead of an output file, not as well as an output file.
Quote:
I really think my teacher should have explained this better to us.
At the beginning of learning file I/O in C, you should have learned basic concepts, such as stdout. Maybe your teacher didn't assign the right reading before this programming assignment. But if you had don't the right reading, statements such as "The program should write to stdout." should understood without any further explanation.
Quote:
Originally Posted by svicino
I didnt use the stdout, I just used a printf,
printf is one of the several output functions that sends its output to stdout. So anywhere you used printf, you did actually use stdout.
Quote:
is there any way to make it like printf("%d", cnt);
Simple
Code:
fprintf(ofp, "%d", cnt);
Quote:
in the output file
Read your assignment carefully. Are you supposed to have an output file? Or are you supposed to output only to stdout?
Read your assignment carefully. Are you supposed to have an output file? Or are you supposed to output only to stdout?
Yes, the question states "Write a program to number the lines in a file. The input file should be passed to the program as a command line argument. The program should write to stdout. Each line in the input file should be written to the output file with the line number and a space prepended."
So I got all of the program to work except for the putc with the number and space, i'm not sure how to do that. I tried the fprintf but i couldn't get that to work either.
The input file should be passed to the program as a command line argument.
Not stating, but I think implying that no output file is passed as a command line argument.
Quote:
The program should write to stdout.
That is quite clear.
Quote:
Each line in the input file should be written to the output file
In context, that is unclear, but I think "the output file" in context means stdout. It does not mean another file.
Quote:
I tried the fprintf but i couldn't get that to work either.
I do very little and none of it recently with formatted C file I/O functions such as fprintf. So I might have given you advice that is just wrong. Also, there might be buffering issues in mixing putc with fprintf. To avoid any buffering issues, you might try fprintf(ofp, "%c", character); instead of putc(character, ofp);
haha, you guys are probly getting sick of these quesitons. But im doing another problem now, it wants me to print 20 lines of code, then wait for a carraige return to show the next 20 lines. I keep getting Segmentation Faults, whats causing these? I don't think i am using fseek right, help?
Code:
#include <stdio.h>
#include <stdlib.h>
void next(FILE *, int *);
int main(char *fileName)
{
FILE *ifp;
int *place, empty, done = 0;
ifp = fopen(fileName, "r");
while(done == 0)
{
next(ifp, place);
printf("More");
scanf("%d", empty);
}
fclose(ifp);
return 0;
}
void next(FILE *ifp, int *place){
int lnCnt, c;
int pla = (int)place;
place = ftell (ifp);
fseek(ifp, pla, SEEK_CUR);
while(lnCnt >20)
{
c =getc(ifp);
printf("%c",c);
if(c == '\n'){
lnCnt++;
}
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.