File to Array and Back.


 
Thread Tools Search this Thread
Top Forums Programming File to Array and Back.
# 1  
Old 02-14-2006
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  
Old 02-14-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. SCO

File permissions reverting back?

After running integrity -e ( may not have done this code thing correctly ) I got the following .io/bootdisk/boot group root should be backup mode 0600 should be 0440 .io/bootdisk/swap group root should be mem ... (3 Replies)
Discussion started by: bigbug
3 Replies

2. Shell Programming and Scripting

Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays. I need to be able to create a loop that will find ${ARRAY1 in the text doc, and replace it with ${ARRAY2 then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2... (2 Replies)
Discussion started by: gentlefury
2 Replies

3. Shell Programming and Scripting

Look back file days in PERL

I have 3 variables , $file =abc_2011_11_01.txt (current day file), $back = Yes and $forward = No I need to search for 3 days back files / 3 days forward files if my current file is not present logic is, I need to download the current day file. If it is missing, i need to look out for currentday... (4 Replies)
Discussion started by: irudayaraj
4 Replies

4. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

5. Shell Programming and Scripting

How to delete a string pattern in a file and write back to the same file

I have a control file which looks like this LOAD DATA INFILE '/array/data/data_Finished_T5_col_change/home/oracle/emp.dat' PRESERVE BLANKS INTO TABLE SCOTT.EMP FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (................. ..................) How can i edit the... (1 Reply)
Discussion started by: mwrg
1 Replies

6. UNIX for Dummies Questions & Answers

Restoring back a deleted file in unix.

Hi, Can any one tell me how to restore back the deleted file in unix? I know the file name. If i know the inode number of the file does help more to restore back the file? (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

7. IP Networking

Back-to-Back Connection using HBAs

Hi every body, Is it possible to connect two servers Back-to-Back (Point-to-Point) using HBA adapters & using Fiber. Note it is direct connection & there is no switches between the servers. I'm concern about using HBA adapters, it is possible or not. Thanks in advance. :) (3 Replies)
Discussion started by: aldowsary
3 Replies

8. Shell Programming and Scripting

How to cut a file name from back side

Hi I have to delete 18 characters from my file names from back side. i mean if file name is abcde123456 then i have to delete 6 characters from back and i need file name as abcde. Please help (4 Replies)
Discussion started by: Prat007
4 Replies

9. AIX

back to back printing in UNIX

Hi , Can you suggest me how to back to back printing in UNIX? Is there any way? Kindly advise. Regards Vijaya Amirtha Raj (3 Replies)
Discussion started by: amirthraj_12
3 Replies
Login or Register to Ask a Question