The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Back-to-Back Connection using HBAs aldowsary IP Networking 3 11-28-2008 10:21 PM
back to back printing in UNIX amirthraj_12 AIX 3 05-06-2008 08:42 AM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 09:42 AM
From File to Array Rock Shell Programming and Scripting 2 02-08-2007 06:10 AM
modifying C file and linking back to project files bruins2005 UNIX for Dummies Questions & Answers 1 06-23-2006 12:08 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-14-2006
Boucho Boucho is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 10
File to Array and Back.

Hi, how can i write a C program in Red Hat 9. I can't find a compiler.
Also would you mind looking at this code and telling me if you think its ok. I need to put a file which has many columns of data (of which i only need the first two) into an array, so that i can add it to another such file. This is prob very basic, but C is a bit of a mystery to me and i'm only beginning to find my way.

#include<stdio.h>
void main()
{
int i,j,array1[][2];
FILE *inp;
inp=fopen("file1.txt","r");
for(i=0;i!=EOF;i++)
for(j=0;j<2;j++)
{
fscanf(inp,"%d",&array1[i][j]);
}
fclose(inp);
}

What do you reckon? How can i show if this worked, would some simple printf command work? Oh, and to put the final array back into a file? Is that just fprintf? Thanks very much for any help you can provide.
  #2 (permalink)  
Old 02-14-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
1. the gcc compiler is distributed as part of RH. try:

Code:
find /usr -name gcc

2. Do not use void main() - it is int main()

Code:
int main( int argc, char *argv[])
{

    ......
    ......
    return 0;   <- last line in main()
}

3. the loop will not work to read a file

Code:
for(i=0;;i++)
{
  test for EOF here
}

so many more errors.... On second thought, you need a C book. try Herbert Schild's book 'Complete C Reference' or one of the SAM's books on C.

As for your problem - it can be done in a few lines of awk code.

Code:
awk ' {print $1, $2}' myfile > newfile

newfile will have the first two columns of myfile.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0