<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Program C in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787996#M44374</link>
    <description>&amp;gt;for(i=0;i&amp;lt;=10;i++){&lt;BR /&gt;&lt;BR /&gt;The correct pattern is:&lt;BR /&gt;for (i=0; i &amp;lt; sizeof(StrArr)/sizeof(StrArr[0]); ++i){&lt;BR /&gt;&lt;BR /&gt;This will do the number of elements in StrArr, not 11.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;fprintf("%s\n",StrArr[j]);&lt;BR /&gt;&lt;BR /&gt;As Steven mentioned, you either use:&lt;BR /&gt;printf("%s\n", StrArr[j]);&lt;BR /&gt;fprintf(stdout, "%s\n", StrArr[j]);&lt;BR /&gt;&lt;BR /&gt;Again, what version of Linux are you using?</description>
    <pubDate>Mon, 16 May 2011 18:22:45 GMT</pubDate>
    <dc:creator>Dennis Handly</dc:creator>
    <dc:date>2011-05-16T18:22:45Z</dc:date>
    <item>
      <title>Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787988#M44366</link>
      <description>Hi there&lt;BR /&gt;I want to read a csv file line by line.&lt;BR /&gt;The program compiles fine but when I'm trying to run it I get "segmentation fault". Could anyone help me to resolve that problem ? &lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STDLIB.H&gt;&lt;BR /&gt;main(){&lt;BR /&gt;char wildcard[10][2];&lt;BR /&gt;char *line = NULL;&lt;BR /&gt;FILE *file;&lt;BR /&gt;file = fopen("/robot/wildcard.csv","r+");&lt;BR /&gt;&lt;BR /&gt;while (!feof(file)){&lt;BR /&gt;     fscanf(file,"%s",line);&lt;BR /&gt;     printf("%s\n",line);&lt;BR /&gt;  }&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;root@robot:/robot# ./file &lt;BR /&gt;Segmentation fault&lt;BR /&gt;&lt;BR /&gt;(gdb) run ./file.c &lt;BR /&gt;Starting program: /robot/file ./file.c&lt;BR /&gt;&lt;BR /&gt;Program received signal SIGSEGV, Segmentation fault.&lt;BR /&gt;0x00007ffff7adbe52 in ?? () from /lib/libc.so.6&lt;BR /&gt;(gdb) &lt;BR /&gt;&lt;BR /&gt;&lt;/STDLIB.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Mon, 16 May 2011 02:29:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787988#M44366</guid>
      <dc:creator>Piotr Kirklewski</dc:creator>
      <dc:date>2011-05-16T02:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787989#M44367</link>
      <description>&lt;!--!*#--&gt;&amp;gt;char *line = NULL;&lt;BR /&gt;&amp;gt;fscanf(file,"%s",line);&lt;BR /&gt;&lt;BR /&gt;You are trying to read into a NULL pointer.  Also, if you are just reading buffers, you should use fgets(3)/fputs(3):&lt;BR /&gt;char buffer[4096];&lt;BR /&gt;while (fgets(buffer, sizeof(buffer), file)) {&lt;BR /&gt;   fputs(buffer, stdout);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&amp;gt;0x00007ffff7adbe52 in ?? /lib/libc.so.6&lt;BR /&gt;&lt;BR /&gt;This is NOT HP-UX.  What OS version are you running?</description>
      <pubDate>Mon, 16 May 2011 02:46:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787989#M44367</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-05-16T02:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787990#M44368</link>
      <description>&lt;BR /&gt;You did not provide a buffer for fscanf&lt;BR /&gt;&lt;BR /&gt;Instead of: char *line = NULL;&lt;BR /&gt;Try : char line[1000];&lt;BR /&gt;&lt;BR /&gt;or use MALLOC : line = malloc ( 1000 );&lt;BR /&gt;&lt;BR /&gt;The 1000 is arbitrary,  pick what you need and CHECK... and check the malloc, and check the OPEN.&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;&lt;BR /&gt;Hein&lt;BR /&gt;</description>
      <pubDate>Mon, 16 May 2011 03:24:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787990#M44368</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2011-05-16T03:24:30Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787991#M44369</link>
      <description>Oops, sorry for the duplicate reply. Walked away for while before hitting submit. Hein&lt;BR /&gt;</description>
      <pubDate>Mon, 16 May 2011 03:26:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787991#M44369</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2011-05-16T03:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787992#M44370</link>
      <description>Piotr, Denis version is far better than using fscanf(), as it get the first line you can parse then.&lt;BR /&gt;( fscanf() don't allocate the "line" memory, so you need to allocate it like fgets)&lt;BR /&gt;&lt;BR /&gt;so a char buff[8192];scanf(file,"%8192s",&amp;amp;buf) will give the first word ( separated by space) and not the first line, ( with a maximum size of 8192&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 16 May 2011 06:30:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787992#M44370</guid>
      <dc:creator>Laurent Menase</dc:creator>
      <dc:date>2011-05-16T06:30:45Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787993#M44371</link>
      <description>Ok &lt;BR /&gt;Thanks to your help I was able to resolve the segmentation error. Now  I can't print the array.&lt;BR /&gt;Could you please help me to figure out why ?&lt;BR /&gt;Reagrds&lt;BR /&gt;&lt;BR /&gt;FILE *fp;&lt;BR /&gt;char *StrArr[5];&lt;BR /&gt;&lt;BR /&gt;fp = fopen("/robot/wildcard.csv", "r+");&lt;BR /&gt;&lt;BR /&gt;int i;&lt;BR /&gt;for(i=0;i&amp;lt;=10;i++){&lt;BR /&gt;fscanf(fp, "%s\n", StrArr[i]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;int j;&lt;BR /&gt;for(j=0;j&amp;lt;=10;j++){&lt;BR /&gt;fprintf("%s\n",StrArr[j]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;root@robot:/robot# gcc -o x x.c &lt;BR /&gt;x.c: In function 'main':&lt;BR /&gt;x.c:34: warning: passing argument 1 of 'fprintf' from incompatible pointer type&lt;BR /&gt;/usr/include/stdio.h:353: note: expected 'struct FILE * __restrict__' but argument is of type 'char *'&lt;BR /&gt;x.c:34: warning: format not a string literal and no format arguments&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 16 May 2011 11:44:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787993#M44371</guid>
      <dc:creator>Piotr Kirklewski</dc:creator>
      <dc:date>2011-05-16T11:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787994#M44372</link>
      <description>Same error. Worse really.&lt;BR /&gt;You declared an array of 5 pointers to strings.&lt;BR /&gt;You allocated none of those strings and try yo use 10.&lt;BR /&gt;It seems you are not ready to tackle this job.&lt;BR /&gt;Please work with a peer to get up to speed.&lt;BR /&gt;&lt;BR /&gt;And.... XML's have been parsed before. Find a package to do the job?&lt;BR /&gt;&lt;BR /&gt;Or is this a training exercise perhpas?&lt;BR /&gt;&lt;BR /&gt;Good luck with your homework,&lt;BR /&gt;&lt;BR /&gt;Hein&lt;BR /&gt;</description>
      <pubDate>Mon, 16 May 2011 12:13:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787994#M44372</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2011-05-16T12:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787995#M44373</link>
      <description>&lt;!--!*#--&gt;&amp;gt; x.c:34: warning: passing argument 1 of 'fprintf' from incompatible pointer type&lt;BR /&gt;&amp;gt; [...]&lt;BR /&gt;&lt;BR /&gt;      man printf&lt;BR /&gt;and/or:&lt;BR /&gt;      man fprintf&lt;BR /&gt;&lt;BR /&gt;Look for differences.&lt;BR /&gt;&lt;BR /&gt;&amp;gt; It seems you are not ready to tackle this job.&lt;BR /&gt;&lt;BR /&gt;He has a point.  Trying to learn basic C&lt;BR /&gt;programming by asking questions like this&lt;BR /&gt;here is not an efficient use of anyone's&lt;BR /&gt;time.</description>
      <pubDate>Mon, 16 May 2011 14:38:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787995#M44373</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2011-05-16T14:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787996#M44374</link>
      <description>&amp;gt;for(i=0;i&amp;lt;=10;i++){&lt;BR /&gt;&lt;BR /&gt;The correct pattern is:&lt;BR /&gt;for (i=0; i &amp;lt; sizeof(StrArr)/sizeof(StrArr[0]); ++i){&lt;BR /&gt;&lt;BR /&gt;This will do the number of elements in StrArr, not 11.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;fprintf("%s\n",StrArr[j]);&lt;BR /&gt;&lt;BR /&gt;As Steven mentioned, you either use:&lt;BR /&gt;printf("%s\n", StrArr[j]);&lt;BR /&gt;fprintf(stdout, "%s\n", StrArr[j]);&lt;BR /&gt;&lt;BR /&gt;Again, what version of Linux are you using?</description>
      <pubDate>Mon, 16 May 2011 18:22:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787996#M44374</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-05-16T18:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787997#M44375</link>
      <description>&lt;BR /&gt;FILE *fp;&lt;BR /&gt;#define SIZEOFSTRARR 10&lt;BR /&gt;char *StrArr[SIZEOFSTRARR];&lt;BR /&gt;int i;&lt;BR /&gt;int j;&lt;BR /&gt;&lt;BR /&gt;fp = fopen("/robot/wildcard.csv", "r+");&lt;BR /&gt;&lt;BR /&gt;for(i=0;i&amp;lt;=SIZEOFSTRARR;i++){&lt;BR /&gt;StrArr[i]=malloc(8193); &lt;BR /&gt;fscanf(fp, "%8192s\n", StrArr[i]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;for(j=0;j&amp;lt;=SIZEOFSTRARR;j++){&lt;BR /&gt;fprintf("%s\n",StrArr[j]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;or&lt;BR /&gt;&lt;BR /&gt;FILE *fp;&lt;BR /&gt;#define SIZEOFSTRARR 10&lt;BR /&gt;char StrArr[SIZEOFSTRARR][8193];/* don't fortget the ending 0*/&lt;BR /&gt;int i;&lt;BR /&gt;int j;&lt;BR /&gt;&lt;BR /&gt;fp = fopen("/robot/wildcard.csv", "r+");&lt;BR /&gt;&lt;BR /&gt;for(i=0;i&amp;lt;=SIZEOFSTRARR;i++){&lt;BR /&gt;fscanf(fp, "%8192s\n", StrArr[i]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;for(j=0;j&amp;lt;=SIZEOFSTRARR;j++){&lt;BR /&gt;fprintf("%s\n",StrArr[j]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;or&lt;BR /&gt;&lt;BR /&gt;FILE *fp;&lt;BR /&gt;#define SIZEOFSTRARR 10&lt;BR /&gt;char *StrArr[SIZEOFSTRARR];&lt;BR /&gt;int i;&lt;BR /&gt;int j;&lt;BR /&gt;&lt;BR /&gt;fp = fopen("/robot/wildcard.csv", "r+");&lt;BR /&gt;&lt;BR /&gt;for(i=0;i&amp;lt;=SIZEOFSTRARR;i++){&lt;BR /&gt;StrArr[i]=malloc(8193); &lt;BR /&gt;fgets( StrArr[i],8193,fp);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;for(j=0;j&amp;lt;=SIZEOFSTRARR;j++){&lt;BR /&gt;fprintf("%s\n",StrArr[j]);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;In any case you need to allocate your buffer before passing it to *scanf/fgets&lt;BR /&gt;Also be sure to use %ns where n is the size of your buffer-1 too be sure you will not overrun your buffer&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;ie&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;main()&lt;BR /&gt;{&lt;BR /&gt;  char buf[1000];&lt;BR /&gt;  scanf("%s",buf);&lt;BR /&gt;  printf("%d",strlen(buf));&lt;BR /&gt;}&lt;BR /&gt;# cc t.c&lt;BR /&gt;# yes | tr "\012" ',' | dd bs=10000 count=1 | a.out&lt;BR /&gt;Memory fault &lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;main()&lt;BR /&gt;{&lt;BR /&gt;  char buf[1000];&lt;BR /&gt;  scanf("%999s\n",buf);&lt;BR /&gt;  printf("%d",strlen(buf));&lt;BR /&gt;}&lt;BR /&gt;# cc t.c&lt;BR /&gt;# yes | tr "\012" ',' | dd bs=10000 count=1 | a.out&lt;BR /&gt;999&lt;BR /&gt;# echo 123456789 123456789 | a.out&lt;BR /&gt;9&lt;BR /&gt;--&amp;gt; it reads only the 1st word!&lt;BR /&gt;&lt;BR /&gt;&lt;/STDIO.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Tue, 17 May 2011 06:06:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787997#M44375</guid>
      <dc:creator>Laurent Menase</dc:creator>
      <dc:date>2011-05-17T06:06:08Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787998#M44376</link>
      <description>Laurent: for (i=0;i&amp;lt;=SIZEOFSTRARR;i++)&lt;BR /&gt;&lt;BR /&gt;Ack!  You have one too many.  That's:&lt;BR /&gt;for (i=0; i &amp;lt; SIZEOFSTRARR; ++i)</description>
      <pubDate>Wed, 18 May 2011 04:35:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787998#M44376</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-05-18T04:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: Program C</title>
      <link>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787999#M44377</link>
      <description>oops yes Denis you are right :D, I didn't see the &amp;lt;= in my copy past, since I don't use it usualy-</description>
      <pubDate>Wed, 18 May 2011 06:57:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/program-c/m-p/4787999#M44377</guid>
      <dc:creator>Laurent Menase</dc:creator>
      <dc:date>2011-05-18T06:57:08Z</dc:date>
    </item>
  </channel>
</rss>

