在我的代码中,我试图用一定长度的文字阅读。但是fscanf对我不起作用。我原本打算使用malloc动态分配内存,但为方便起见我切换到静态。我的问题是在copyWords函数中,靠近底部?我的问题是什么?
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
char longestWords[15][100];
char exampleLetters[] = "faeugher"; //These are a group of example letters that will be randomly generated in a previous part of the program
char *fileName = "D:\\webster.txt"; //Dictionary file
//int initialiseWords(); //This was to set my pointers to NULL if I was to use malloc
int copyWords(int val);
int main() //This function detects how many words there are of each length (maximum length is 8)
{
int i, j, k;
long int n[8] = {0};
char line[10];
FILE *fp = fopen(fileName, "r");
//initialiseWords();
if (fp == NULL) {
printf("Error opening file!\n");
}
else
{
while (fgets(line, sizeof(line), fp)) {
k = 0;
for (i = 0; i < 8; i++) {
for (j = k; j < 8; j++) {
if (line[i] == exampleLetters[j]) {
k++;
break;
}
}
}
for (i = 1; i < 9; i++) {
if (k == i) n[i-1]++; //These values are incremented everytime a word of that amount of letters is found i.e. n[0]++ when a one letter word is found
}
}
}
fclose(fp);
for (i = 7; i >= 0; i--) {
if (n[i] != 0) {
copyWords(i);
break;
}
}
for (i = 0; i < 8; i++) {
printf("%li ", n[i]); //This is irrelevant but just to display the amount of each number of words
}
return 1;
}
/*int initialiseWords()
{
int i;
for (i = 0; i < 100; i++) {
longestWords[i] = NULL;
}
return 1;
}*/
int copyWords(int val) //This function copies of over the words that have the maximum length
{
int i, j, k, l;
char line[15];
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
printf("Error opening file!\n");
}
else
{
l = 0;
while (fgets(line, sizeof(line), fp)) {
k = 0;
for (i = 0; i < 8; i++) {
for (j = k; j < 8; j++) {
if (line[i] == exampleLetters[j]) {
k++;
break;
}
}
}
if (k == val + 1) {
fscanf(fp, "%s", &longestWords[l]); //MY PROBLEM!!! The data won't read in to longestWords
l++;
}
}
}
fclose(fp);
return 1;
}
答案 0 :(得分:0)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<div class="lnr lnr-menu"></div>
</button>
<a class="navbar-brand logo" href="#">sample</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="#Button1">Button 1</a>
</li>
<li><a href="#Button2">Button 2</a>
</li>
<li><a href="#Button3">Button 3</a>
</li>
<li><a href="#Button4">Button 4</a>
</li>
<li><a href="#Button5">Button 5</a>
</li>
<li><a href="#Button6">Button 6</a>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div class="container-fluid">
<div class="content">
<section id="Button1"></section>
<section id="Button2"></section>
<section id="Button3"></section>
<section id="Button4"></section>
<section id="Button5"></section>
<section id="Button"></section>
</div>
</div>
我决定将最长词改为双指针。然后我根据该类型数组中的多少个单词为此指针分配内存,例如如果有3个八个字母的单词(n [7]),我会给双指针分配三个指针,然后我指定了9个字符的指针。这允许我strcpy()行到最长的词并打印它们。