From a file into an array.

This is a discussion on From a file into an array. within the C Programming forums, part of the General Programming Boards category; I am trying to read the first value of a file, allocate an array of the amount equal to the ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    From a file into an array.

    I am trying to read the first value of a file, allocate an array of the amount equal to the first value, then read the rest (the number of places already defined) of the file into that array. I have already done something similar using file pointers:

    Code:
    int main(int argc, char *argv[])
    {
    
    	int numberOfChar;
            int thisInt;
    	int currentInt;
    
    
    
       	FILE *fp;
    	fp=fopen("test.txt", "rb");
    	if (fscanf(fp, "%d", &numberOfChar) == 1)
    	printf("%d\n", numberOfChar);
    	
    
    
    for(currentInt = 0; currentInt < numberOfChar; currentInt++)
    	{
    	fscanf(fp, "%d", &thisInt);
    	printf("%d\n", thisInt);
    	
    }
    
    	printf("\n");
    	printf("\n");
    	
    
     return 0;
    }

    But I'm trying to do it with arrays now, so as to manipulate the array afterward. I can read the first int of the file and allocate the array:

    Code:
        int numberOfChar;
    
       	FILE *fp;
    	fp=fopen("test.txt", "rb");
    	if (fscanf(fp, "%d", &numberOfChar) == 1)
    	
    	int x[numberOfChar]; //making array x have a size of numberOfChar
    But reading the rest of the file, only to the numberOfChar, I can't figure out. I have seen arrays being made in C++ from user input:

    Code:
    int s;
    
    	for (s = 0; s < numberOfChar; s++)
       	{
       	cout << "Enter an integer: ";
       	cin >> A[s];
       	}
    
    	for (s = 0; s < numberOfChar; s++)
       	cout << A[s] << endl;
    But I am trying to do it from a file, in C. I believe it will be a similar for loop though. I believe I have it allocated right, but how do I keep writing data into the array? Thank you in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So instead of cin >> A[s], do fscanf("%d", &A[s]). What is difficult here?

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    I can do it from user input, the difficult part is reading it from a file.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fmsguy06 View Post
    I can do it from user input, the difficult part is reading it from a file.
    If it was difficult, you wouldn't have this:
    Code:
    if (fscanf(fp, "%d", &numberOfChar) == 1)
    in your code, would you?

    Why would reading from a file magically change from here to three lines later?

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21
    Sure that read from the file, but placing what I read into the array is what's not working.

    Code:
    int main(int argc, char *argv[])
    {
        int numberOfChar;
    
    
       	FILE *fp;
    	fp=fopen("test.txt", "rb");
    	if (fscanf(fp, "&#37;d", &numberOfChar) == 1)
    	printf("%d\n", numberOfChar);
    	int x[numberOfChar];
    
    	
    	int s;
    
    	for (s = 0; s < numberOfChar; s++)
       	{
    
    	fscanf("%d", &x[s]);
    
    
            //Type error in argument 1 to 'fscanf'; found 'char *', expected 'FILE *'.
            //Type error in argument 2 to 'fscanf'; found 'int *', expected 'const char *'
    
    
       	}
    
    
    
        return 0;
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well you made a decent job of using fscanf earlier on in the code.
    Why now the difficulty in adding the correct parameters?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    21
    I changed one line:
    Code:
    fscanf(fp, "&#37;d", &x[s]);
    so now:
    Code:
    int main(int argc, char *argv[])
    {
        int numberOfChar;
    
    
       	FILE *fp;
    	fp=fopen("test.txt", "rb");
    	if (fscanf(fp, "%d", &numberOfChar) == 1)
    	printf("%d\n", numberOfChar);
    	int x[numberOfChar];
    
    	
    	int s;
    
    	for (s = 0; s < numberOfChar; s++)
       	{
    	fscanf(fp, "%d", &x[s]);
       	}
    
    
        return 0;
    }
    It returns the number 3... which is the first number of the text document...so I think I'm heading in the right direction, but still not entering the data into the array?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    How about testing the result of fscanf() inside the loop as well?

    Also, paste your text file containing the numbers you want to read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    21
    Contents of test.txt:



    15 475 3260 7102 6004 8467 7024 1819 2901 3844 3613 640 7549 3096 3365 771 2759 4615 966 1021 8203 874 128 6330 2772 9943 2474 168 5536 113 737 6803 8409 6687 1490 6220 1903 161 8108 5600 2356 1455 7227 7962 327 2536 6349 4573 7514 2191 5379 4858 9925 9807 2462 4563 6366 1642 6815 6618 2257 9824 108 1677 4840 990 2670 9355 4491 492 5899 3972 3074 6736 6872 690 6142 9890 9524 6589 6683 5443 4048 8359 4948 5717 8594 8800 9676 1860 7701 5810 4808 7992 4771 7748 5657 6708 4801 4933 2731 4480 8242 4689 7135 9712 3304 909 7285 7719 3379 6301 8159 9278 7681 6479 1380 2484 3178 327 6197 9626 8980 5056 459 5084 6610 7125 6258 3080 6867 5399 695 434 3673 2688 4806 8395 7450 4795 3740 9276 9897 8357 195 2515 7974 1552 2014 4156 208 3590 9619 9249 5847 5479 9587 6125 7861 7092 4544 8834 4110 4761 8473 131 7039 1640 1291 8079 1773 8996 7954 3745 996 6851 9572 5311 59 2951 8415 8577 4948 6244 4794 3802 6317 2106 3654 1887 6886 9259 4989 860 1738 4717 3574 1997 1967 7009 2096 8195 6086 2590 1183 997 9332 7086 7268 1906 3975 1608 2602 3774 3821 5624 5389 9514 9065 3121 1748 7825 6556 1036 3119 9846 104 6786 2672 4671 2472 3221 2504 3850 9801 5651 5716 975 2171 6804 98 7630 3063 6098 4722 7335 7234 2692 1900 4411 9926 5621 785 6836 7598 4018 2493 5258 7994 6506 8826 6819 4623 2247 945 1233 3786 2477 486 9857 6274 9841 7315 2431 2647 9089 5209 5565 60 4542 4318 9774 6370 4841 675 5630 2114 1134 256 670 9064 7042 2129 2358 1743 2831 5505 8879 1015 7543 8929 7062 8825 3535 9784 635 1511 1659 3282 8446 1442 697 1940 8327 6497 9266 3272 8303 8528 2287 6596 6698 6900 6937 9384 7138 8540 8539 7667 9583 782 4989 1837 6570 7627 1805 4107 1877 2889 4206 1651 8966 405 2793 5894 7265 1495 886 8874 6069 5342 3219 7962 9452 5783 5013 5085 8018 7072 6268 7314 2354 8528 5045 4283 1105 743 3007 2518 8912 5605 7985 9620 8652 4094 9377 2924 5174 1737 9701 3404 9201 6541 7526 534 5112 5747 4852 2540 5597 5630 301 6597 3068 2163 427 5250 4097 1785 5986 2936 3698 1634 7255 714 2232 8633 884 8280 8832 4865 7402 744 8098 3518 2796 3804 2746 522 7035 4683 5953 429 5969 5407 3642 211 9390 5189 2107 4398 6205 455 3122 2206 5547 5134 6713 2229 3147 6665 4166 2413 2851 393 7303 3207 8492 7112 6510 7992 1506 2723 5023 6601 9921 7384 4309 5085 5203 3654 4625 7811 2607 5440 1869 7901 8139 6939 6372 3293 703 5411 5126 6814 9250 9472



    I added one more line, making the code:

    Code:
    int main(int argc, char *argv[])
    {
        int numberOfChar;
    
    
       	FILE *fp;
    	fp=fopen("test.txt", "rb");
    	if (fscanf(fp, "&#37;d", &numberOfChar) == 1)
    	printf("%d\n", numberOfChar);
    	int x[numberOfChar];
    
    	
    	int s;
    
    	for (s = 0; s < numberOfChar; s++)
       	{
    
    	fscanf(fp, "%d", &x[s]);
    	printf("%d\n", x[s]);
       	
    	}
    
    
        return 0;
    }
    Which prints out the first 15 characters:

    15
    475
    3260
    7102
    6004
    8467
    7024
    1819
    2901
    3844
    3613
    640
    7549
    3096
    3365
    771

    (Though I wish it would print horizontal not vertically)


    but the array is working, I believe.
    Last edited by fmsguy06; 11-09-2008 at 01:06 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fmsguy06 View Post

    (Though I wish it would print horizontal not vertically)
    If you want it on one line, why are you printing out a \n after each number then?

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    21
    Good point. Is there a way to limit the number printed per line? I thought I could use:

    Code:
    printf("&#37;10d ", x[s]);
    but that just prints a big mess...

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to decide when you want to print the new line.

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    21
    Every ten characters is what I tried, but that apparently doesn't work, at least not that way.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    if ( numOfSomething == 10 ) print a newline, and reset counter
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    May 2007
    Posts
    21
    Neither did:

    Code:
    ....
    
    for (s = 0; s < numberOfChar; s++)
       	{
    
    	int n = 1;
    	fscanf(fp, "&#37;d", &x[s]);
    	printf("%d ", x[s]);
    
    	if (n == 10)
    	{
    	printf("\n");
    	
    		}
    	n++;
       	
    	}
    ....
    still prints all 15.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21