Sponsored Content
Full Discussion: Memory fault(coredump)
Homework and Emergencies Homework & Coursework Questions Memory fault(coredump) Post 302552520 by kermit on Sunday 4th of September 2011 07:55:43 AM
Old 09-04-2011
Code:
#include <stdio.h>

main()
{
        int c;
    int nl_cnt = 0;

        while((c = getchar()) != EOF){

                if(c == '\n'){
                nl_cnt++;
                printf("%d ", nl_cnt);}

                putchar(c);

        }

}

Quote:
Originally Posted by heywoodfloyd
how do I get the numbers and the code to line up correctly?
Your logic is a bit off. Check if c is a newline. If it is, print c, and then print a line number. Otherwise, just print c. You might also want to print a line number before your loop so that your first line is numbered.

A couple of other things:
  1. You should have warnings turned on for your compiler. This would have helped you catch your initial problem much easier. For example, if you were using gcc to compile, you would use (at minimum) the -Wall and -Wextra switches, e.g.,
    Code:
    gcc -Wall -Wextra -o myprog myprog.c

    Consult your manual pages for specific details about your system.
  2. The second is more nitpicky, but still good to know. The above program is not exactly conforming C - not to C89, and not to C99. In C89, main may be declared as above, but must have an explicit return statement. Under C99, main must be declared as one of the following:
    Code:
    int main(void)
    int main(int argc, char *argv[]) /* int main(int argc, char **argv)
                                 * is also acceptable
                                 */

    Additionally, under C89, an explicit return at the end of main is required. In C99, it is not, and if not provided, a return value of zero is implied. Had you turned your compiler warnings on, it may have told you that you did not return a value from main in your 'cat -n' program.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Coredump (memory fault)

We are running a SQR program on Unix Platform with Oracle RDBMS. It's an interfacing program to integrate data from foreign sites to PeopleSoft database, using a flat file input. After many hours of processing, the program stops with a coredump error (memory fault). With top command we noticed... (1 Reply)
Discussion started by: araziki
1 Replies

2. Programming

Memory fault(coredump)

Dear All, I made a program which do some simple jobs like reading data from other process's shared memory and writing messages to the queues of other process. what happens is my program works fine and do all the task as expected but then then program ends it give Memory fault(coredump). I... (0 Replies)
Discussion started by: ralo
0 Replies

3. AIX

Segmentation fault(coredump)

Hi All Can anybody help me? When ever am trying to run topas system gives me an error Segmentation fault(coredump) does anybody ahve solution for this? (4 Replies)
Discussion started by: vjm
4 Replies

4. Ubuntu

Memory fault(coredump)

Hey guys, I am new to the Linux world and have a question to post. When I ssh from a HP-UX machine to a ubuntu machine I get the following error message Memory fault(coredump) i.e. ssh 192.168.1.3 I get this message as shown below Memory fault(coredump) Can someone please explain... (2 Replies)
Discussion started by: fkaba81
2 Replies

5. Solaris

memory fault (coredump)

Getting memory fault (coredump) and segmentation fault(coredump) when i tried javac or java -version. what could be the problem? Regards Eswar (2 Replies)
Discussion started by: BhuvanEswar
2 Replies

6. Shell Programming and Scripting

Segmentation Fault(coredump)

I'm getting this error when trying to run a Acucobol program thru UNIX.. Segmentation Fault(coredump) Precompiler error prevents compilation of xxxxxx.co. Please help me in this case.. (1 Reply)
Discussion started by: Manish4
1 Replies

7. Programming

Memory fault(coredump) while using Environment variables

Hi, Actually I am facing one issue while using the getenv() in the C/C++ program. I want to take the file path from environment variables and if am not defining the environment path, its showing the message like this…! Memory fault(coredump) Actually I want to handle the error ,... (6 Replies)
Discussion started by: Kattoor
6 Replies

8. Shell Programming and Scripting

Memory fault(coredump) while using cat cmd

i have few log files that are input to my perl script... i am executing the script as below cat RTR*.log | test.pl and getting the following error -ksh: 25014: Memory fault(coredump) cat: write error: Connection reset by peer can anyone help me on this.... Thanks in... (2 Replies)
Discussion started by: niteesh_!7
2 Replies

9. AIX

Error: Memory fault(coredump)

Hi Experts, While running a command, i saw this error below # ls -lrt total 74008 -rw-r--r-- 1 rr57104 edcfes 37889134 May 16 12:41 LGTOnw.clnt.7.4.2.0.bff.tar.gz drwxr-xr-x 2 root system 256 May 18 12:42 lost+found # gunzip LGTOnw.clnt.7.4.2.0.bff.tar.gz Memory fault(coredump) ... (2 Replies)
Discussion started by: EngnrRG
2 Replies

10. UNIX for Advanced & Expert Users

Memory fault(Coredump)

Hi, In my application we have one job which is used to process the files. But that job is failing with memory fault while processing a file or while shutting down the job. Sometime it generates the coredump and sometimes not. When I analysed the core dump I got below code snippet where it... (3 Replies)
Discussion started by: shilpa_20
3 Replies
ARG(2)								System Calls Manual							    ARG(2)

NAME
ARGBEGIN, ARGEND, ARGC, ARGF, arginit, argopt - process option letters from argv SYNOPSIS
#include <u.h> #include <libc.h> ARGBEGIN { char *ARGF(); Rune ARGC(); } ARGEND extern char *argv0; /* Alef only */ Arg *arginit(int argc, byte **argv); Rune argopt(Arg *arg); byte *argf(Arg *arg); DESCRIPTION
These macros assume the names argc and argv are in scope; see exec(2). ARGBEGIN and ARGEND surround code for processing program options. The code should be the cases of a C switch on option characters; it is executed once for each option character. Options end after an argu- ment --, before an argument -, or before an argument that doesn't begin with -. ARGC() returns the current option character. ARGF() returns the current option argument: a pointer to the rest of the option string if not empty, or the next argument in argv if any, or 0. ARGF must be called just once for each option that takes an argument. After ARGBEGIN, argv0 is a copy of argv[0] (conventionally the name of the program). After ARGEND, argv points at a zero-terminated list of the remaining argc arguments. Alef The Alef argument processing routines are unrelated. Instead, an aggr called Arg is initialized by a call to arginit. Successive calls to argopt return successive option characters, or zero at the end of the options. After a call to argopt, argf will return any argument string associated with the option. EXAMPLES
This C program can take option b and option f, which requires an argument. #include <u.h> #include <libc.h> void main(int argc, char *argv[]) { char *f; print("%s", argv[0]); ARGBEGIN { case 'b': print(" -b"); break; case 'f': print(" -f(%s)", (f=ARGF())? f: "no arg"); break; default: print(" badflag('%c')", ARGC()); } ARGEND print(" %d args:", argc); while(*argv) print(" '%s'", *argv++); print(" "); exits(0); } Here is the output for the run prog -bffile1 -r -f file2 arg1 arg2 prog -b -f(file1) badflag('r') -f(file2) 2 args: 'arg1' 'arg2' This Alef program accepts options b and, with an attached file name, f. #include <alef.h> void main(int argc, byte **argv) { int a, ac, bflag; byte *file; Arg *arg; arg = arginit(argc, argv); while(ac = argopt(arg)) switch(ac){ case 'b': bflag = 1; break; case 'f': file = argf(arg); break; } for(a=0; a<arg->ac; a++) print("argument %s ", arg->av[a]); } SOURCE
/sys/include/libc.h ARG(2)
All times are GMT -4. The time now is 03:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy