2 Replies - 15635 Views - Last Post: 01 February 2011 - 01:42 PM Rate Topic: -----

#1 xristosmr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 11

Reading a file into two arrays using C

Posted 01 February 2011 - 01:07 PM

i want a program using C that can read a file into two arrays,one for noumbers and one for letters...thats my code

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    char gr[1000];
    int no[1000];
    int i;
    FILE *fp;
    
    fp = fopen("profit.in","r");
    for (i=0; i<10; i++)
    {
    fscanf(fp,"%d",&no[i]);
    printf("nombers: %d\n",no[i]);
    fscanf(fp,"%s",&gr[i]);
    printf("letters:%s",gr[i]);
    }
    fclose(fp);
    getchar();
    return 0;
}

  



But when i am running it, it stops...do you have any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Reading a file into two arrays using C

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: Reading a file into two arrays using C

Posted 01 February 2011 - 01:29 PM

Moved from the VB Tutorials Section to C/C++.

It would also help if you described your problems or errors.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 3,000
  • Joined: 30-May 10

Re: Reading a file into two arrays using C

Posted 01 February 2011 - 01:42 PM

> printf("letters:%s",gr[i]);
%s prints strings, not characters.

> fscanf(fp,"%s",&gr[i])
I'm guessing you wanted to read a string here as well?

Perhaps
char gr[1000][10];
if ALL your strings are <= 9 characters in length would be a start.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1