Visit Our UNIX and Linux User Community


import .txt and split word into array C


 
Thread Tools Search this Thread
Top Forums Programming import .txt and split word into array C
# 1  
Old 10-03-2011
import .txt and split word into array C

Hi, if I want to import .txt file that contain information and the number separate by space how can I split and put into array In C

Example of .txt file
Code:
3 Aqaba
49789 10000 5200 25.78
6987 148976 12941 15.78
99885 35262 2501 22.98

Thank

Last edited by Scott; 10-03-2011 at 12:45 PM.. Reason: As it's homework, thread closed
# 2  
Old 10-03-2011
You need to look at the manpage of strtok as it will spilt up each line based on the field delimiter...which in this case will be a space character and post how the output should look like...
# 3  
Old 10-03-2011
Code:
#include <stdio.h> 

int DisplayFile(char filename[]) 
{ 
    FILE *f; 
    int     ch;  /* int or signed char. See below. */ 
    f = fopen(filename, "r"); 
    if (f == NULL) { 
        /* Couldn't open the file. */ 
        printf("Input file not found.\n"); 
        return -1; /* Non-zero means error. */ 
    } 
    /* Display the entire file on the screen */ 
    ch = fgetc(f); 
    while (ch != EOF) 
    { 
        ch = strtok(filename, " ");
        putchar(ch);  /* Display one char on screen */ 
        ch = fgetc(f); 
    } 
    return 0; 
} 
int main(void) 
{ 
    int   result; 
    result = DisplayFile("fadata.rtf"); 
    return result; 
}

it not working

I just want split and store into array or make it variable to use to calculate other thing

Last edited by guidely; 10-03-2011 at 11:17 AM..
# 4  
Old 10-03-2011
I notice you're spamming multiple topics. I'll reply in your other one.

Previous Thread | Next Thread
Test Your Knowledge in Computers #920
Difficulty: Medium
The Unix time number is zero at the Unix epoch, and increases by exactly 86400 per day since the epoch.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array - Export/Import in global environment variables.

Hello. During startup /etc/bash.bashrc.local generates some array ..... source /.../.../system_common_general_array_env_var ..... The file system_common_general_array_env_var contains : LEAP_VERSION='42.3' ARRAY_MAIN_REPO_LEAP=('zypper_local' 'openSUSE-Leap-'"$LEAP_VERSION"'-Non-Oss' ... (2 Replies)
Discussion started by: jcdole
2 Replies

2. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Windows & DOS: Issues & Discussions

SQL Server import csv or txt on a Solaris box

New to using sql server and unix, but say I have here /home/foo/file.txt Can I use some kind of process to push that .txt into a sql server? Or Is there a sql server utility that can be configured to find a unix box and go into /home/foo/file.txt and slurp it up:) Or is there any PC and... (5 Replies)
Discussion started by: sas
5 Replies

5. Shell Programming and Scripting

How to split this txt file into small files?

Dear shell experts, I would like to spilt a txt file into small ones. However, I did not know how to program use shell. If someone could help, it is greatly appreciated! Specifically, I supposed there is file named A.txt. The content of the file likes this: Subject run condtion ACC time... (3 Replies)
Discussion started by: psychmyluo
3 Replies

6. Shell Programming and Scripting

How to import a value from txt file

Hello, I want to give a value from a txt file to my variable at my ksh script. I`ve searched on the net and I found the command variable < file.txt but it cannot see the file. The .txt file contains two values in the first and the second line. Is there any way to give the first-line value to... (1 Reply)
Discussion started by: chris_euop
1 Replies

7. Shell Programming and Scripting

KSH script for split a txt file

I have a problem which I would like to solve by using UNIX power and inspired minds around world. Here is the problem I have a text file and it has data as follows 1X.....................1234567890123456789T1234598765XT1 (header) 1Z01............(sub HEADER) P100001............ Q1........... (4 Replies)
Discussion started by: ask.chowhan
4 Replies

8. Shell Programming and Scripting

shellscript to read data from txt file and import to oracle db

Hi all, Help needed urgently. I am currently writing a shellscript to read data/record from a flat file (.txt) file, and import/upload the data to oracle database. The script is working fine, but it takes too long time (for 18000 records, it takes around 90 mins). I guess it takes so long... (1 Reply)
Discussion started by: robot_mas
1 Replies

9. Shell Programming and Scripting

Shell snip to import CSV data into BASH array

I have been trying to write a simple snip of bash shell code to import from 1 to 100 records into a BASH array. I have a CSV file that is structured like: record1,item1,item2,item3,item4,etc.,etc. .... (<= 100 items) record2,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)... (5 Replies)
Discussion started by: dstrout
5 Replies

10. Shell Programming and Scripting

split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be?? e.g. running time strings through an array and trying to examine just minutes: 12:25:30 10:15:13 08:55:23 awk ' NR==FNR{ ... (2 Replies)
Discussion started by: dcfargo
2 Replies

Featured Tech Videos