LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2010, 10:12 AM   #1
svicino
LQ Newbie
 
Registered: May 2010
Posts: 8

Rep: Reputation: 0
C Help, maybe i over looked something?


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:

#include <stdio.h>
#include <stdlib.h>

void new_line(FILE *, FILE *);

int main(char *argv)
{

FILE *ifp, *ofp;
char *stout;


ifp = fopen(argv, "r");
ofp = fopen(stout, "w");
new_line(ifp, ofp);
fclose(ifp);
fclose(ofp);
return 0;
}

void new_line(FILE *ifp, FILE *ofp)
{
int c, cnt = 1;

while((c = getc(ifp)) != EOF) {
putc(c, ofp);
if (c == '\n')
putc(cnt, ofp);
cnt++;
}
}
 
Old 05-13-2010, 11:00 AM   #2
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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.

Hope this helps.
 
1 members found this post helpful.
Old 05-13-2010, 11:00 AM   #3
svicino
LQ Newbie
 
Registered: May 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 05-13-2010, 11:03 AM   #4
svicino
LQ Newbie
 
Registered: May 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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."

Last edited by svicino; 05-13-2010 at 11:09 AM.
 
Old 05-13-2010, 11:22 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by svicino View Post
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.

Here is some online documentation of stdout that might help you understand:
http://www.cplusplus.com/reference/c...cstdio/stdout/

(It is a C++ site, but a C section of the C++ documentation, so it does apply to C).

Last edited by johnsfine; 05-13-2010 at 11:30 AM.
 
Old 05-13-2010, 11:24 AM   #6
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Quote:
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"

Ciao!

Last edited by rstewart; 05-13-2010 at 11:27 AM.
 
1 members found this post helpful.
Old 05-13-2010, 11:31 AM   #7
svicino
LQ Newbie
 
Registered: May 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post

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:

Code:
 #include <stdio.h>
#include <stdlib.h>

void    new_line(FILE *, FILE *);

int main(int argc, char *argv[])
{
        FILE    *ifp, *ofp;

        ifp = fopen(argv[0], "r");
        ofp = fopen(argv[1], "w");
        new_line(ifp, ofp);
        fclose(ifp);
        fclose(ofp);
        return 0;
}

void new_line(FILE *ifp, FILE *ofp)
{
        int c, cnt = 1;

        while((c = getc(ifp)) != EOF) {
                putc(c, ofp);
                if (c == '\n')
                putc(cnt, ofp);
                cnt++;
        }
}
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?
 
Old 05-13-2010, 11:36 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by svicino View Post
Code:
        ifp = fopen(argv[0], "r");
        ofp = fopen(argv[1], "w");
argv[0] is the filename of the running program.
argv[1] is the first parameter. (Notice rstewart already explained that in post#2)

Also, you don't seem to have tried anything to use stdout for the output.

Quote:
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?
You are reading the program itself and writing to the file specified by the first parameter.

IIUC, you are supposed to read the specified file and write to stdout.

Last edited by johnsfine; 05-13-2010 at 11:37 AM.
 
Old 05-13-2010, 11:43 AM   #9
svicino
LQ Newbie
 
Registered: May 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post
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.

Here is some online documentation of stdout that might help you understand:
http://www.cplusplus.com/reference/c...cstdio/stdout/

(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.
 
Old 05-13-2010, 12:11 PM   #10
svicino
LQ Newbie
 
Registered: May 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Thank you both very much, I got my program to run finally!

Code:
#include <stdio.h>
#include <stdlib.h>

void    new_line(FILE *, FILE *);

int main(int argc, char *argv[])
{
        FILE    *ifp, *ofp;

        if(argc > 3)
        {
        printf("Error, you did not provide the correct command line arguments!\nPlease make sure you entered the input and output files!(output file dose not have to exist!)");
        }

        ifp = fopen(argv[1], "r");
        ofp = fopen(argv[2], "w");
        new_line(ifp, ofp);
        fclose(ifp);
        fclose(ofp);
        return 0;
}

void new_line(FILE *ifp, FILE *ofp)
{
        printf("Writing to stdout:\n");
        int c, cnt = 1;

        while((c = getc(ifp)) != EOF) {
                putc(c, ofp);
                printf("%c", c);

                if (c == '\n')
                {
                putc(cnt, ofp);
                cnt++;
                printf("%d", cnt);
                }
        }
}
I didnt use the stdout, I just used a printf, but I dont think there should be a problem thier. I just had one last question, with the
Code:
 putc(cnt, ofp);
is there any way to make it like
Code:
 printf("%d", cnt);
becuase in the output file the numbers bisplay as random characters, not the number.
 
Old 05-13-2010, 12:35 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
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.
 
Old 05-13-2010, 01:17 PM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by svicino View Post
"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 View Post
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?
 
Old 05-13-2010, 01:39 PM   #13
svicino
LQ Newbie
 
Registered: May 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post
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.
 
Old 05-13-2010, 02:09 PM   #14
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by svicino View Post
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);
 
Old 05-13-2010, 03:05 PM   #15
svicino
LQ Newbie
 
Registered: May 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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++;
                        }
                }

}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
newbies being looked down upon ajzebuski General 15 10-29-2007 04:32 AM
ltmodem - please help, ive looked everywhere.... tbfirefox Linux - Hardware 8 07-11-2004 08:50 PM
Need Help - Looked Over Threads inescapeableus Linux - Newbie 5 04-15-2004 03:05 PM
Ive looked around at different distros.... now can someone characterize one for me? silverbullet Linux - Newbie 5 11-30-2003 05:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration